【发布时间】:2018-09-12 20:20:15
【问题描述】:
单击以获取新报价时,整个页面的颜色都会发生变化,包括按钮。但是,如果用户将鼠标放在按钮上(悬停),颜色不会改变。搞砸了整个设计。
你知道我该如何解决这个问题吗?
$("#getQuote").html(function() {
$.getJSON(
"https://random-quote-generator.herokuapp.com/api/quotes/",
function(val) {
var ran = Math.floor(Math.random() * 80);
$(".cont").html(val[ran].quote);
$(".title").html("- " + val[ran].author);
}
);
});
$("#getQuote").on("click", function() {
$.getJSON(
"https://random-quote-generator.herokuapp.com/api/quotes/",
function(val) {
var ran = Math.floor(Math.random() * 80);
$(".cont").html(val[ran].quote);
$(".title").html("- " + val[ran].author);
}
);
});
// Twitter share button
$("#tweetIt").on("click", function() {
var quote = document.getElementById("result").innerText;
var author = document.getElementById("title").innerText;
var tweetUrl =
"https://twitter.com/share?text=" +
encodeURIComponent(quote + " " + author + " #quote") +
"&url=" +
"www.qod.com/";
window.open(tweetUrl);
});
$(document).ready(function() {
$("#getQuote").click(function() {
var col = Math.floor(Math.random() * 12);
var color = [
"#16a085",
"#27ae60",
"#2c3e50",
"#f39c12",
"#e74c3c",
"#9b59b6",
"#FB6964",
"#342224",
"#472E32",
"#BDBB99",
"#77B1A9",
"#73A857"
];
$(".twitter, #tweetIt")
.css({
background: color[col],
"border-radius": "3px"
})
.addClass("animated fadeIn 8000");
$("i, span, p, .quote")
.css("color", color[col])
.addClass("animated fadeIn");
$(".quote").hover(
function() {
$(this).css({
background: color[col],
"border-radius": "4px"
});
$(this).css("color", "white");
},
function() {
$(this).css("background-color", "white");
$(this).css("color", color[col]);
}
);
$("#tweetIt").hover(
function() {
$(this).css("color", "white");
$(this).css("transform", "scale(1.1,1.1)");
},
function() {
$(this).css("color", "white");
$(this).css("transform", "scale(1,1)");
}
);
setTimeout(function() {
$("i, span, p, background").removeClass("animated fadeIn");
}, 500);
});
});
.container {
margin-top: 200px;
}
.row {
height: auto;
}
.test4 {
position: relative;
bottom: 10;
right: 0;
}
.iconLeft {
float: right;
right: 16px;
font-size: 5em;
color: #555555;
}
.iconRight {
float: bottom right;
right: 16px;
font-size: 5em;
color: #555555;
}
.cont {
font-size: 2em;
color: #555555;
}
.title {
text-align: right;
font-size: 1.3em;
margin-top: 1em;
color: #555555;
}
.main {
display: flex;
justify-content: center;
}
.quote {
width: 300px;
border-radius: 3px;
margin-top: 20px;
font-size: 20px;
padding: px;
height: 60px;
line-height: 0px;
background: white;
color: #555555;
margin-bottom: 300px;
border: solid 2px;
}
.quote:hover {
background: #555555;
color: white;
border-radius: 4px;
}
.twitter {
float: left;
color: white;
}
.twitter:hover {
transform: scale(1.1, 1.1);
color: white;
}
button {
background-color: #555555;
font-size: 3em;
}
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
<div class='row'>
<div class='col-sm-2 test2'>
<i class="fa fa-quote-left iconLeft"> </i>
</div>
<div class='col-sm-8 test3'>
<span class="cont" id="result" onclick="myFunction()"></span>
<p class="title" id="title"></p>
<div class="twitter">
<button id="tweetIt" class="btn fab fa-twitter" title="Tweet this quote!"> Tweet</button>
</div>
</div>
<div class='col-sm-2 test4'>
<i class="fa fa-quote-right iconRight"> </i>
</div>
</div>
</div>
<div class="row container-fluid main">
<button id="getQuote" class="quote btn btn-outline-secondary">New Quote</button>
</div>
【问题讨论】:
-
也许您需要刷新 div/ 元素? stackoverflow.com/questions/1401603/…
-
颜色变化对我来说很好
-
@Huangism for me (on chrome) 如果鼠标光标保持在按钮上,同时单击它不会刷新按钮颜色。
-
那是因为新的
mouseenter事件没有发生...你至少可以在点击时触发mouseleave事件...让按钮变白。 -
一个通用注释,这几乎都应该在 CSS 中完成,并且使用 JS 只是为了更改将定义主题的类。
标签: javascript jquery html css