简体   繁体   English

JS淡入,淡出麻烦

[英]JS fadeIn, fadeOut troubles

I have a pure CSS rollover that I'm trying to add a smooth transition/fade effect to and I can't seem to get it to go. 我有一个纯CSS过渡,正试图向其添加平滑的过渡/淡入淡出效果,但似乎无法实现。

When you hover over the div "online", a new div "online-hover" appears. 当您将鼠标悬停在div“在线”上时,会出现一个新的div“在线悬停”。 "Online" is the default with a background image, then the new div "online-hover" should fade in on top with a new background image. “在线”是带有背景图片的默认设置,然后新的div“在线悬停”应该在顶部带有新的背景图片。 I have the HTML & CSS working, but now I'm trying to add JS to make it look pretty. 我可以使用HTML和CSS,但现在我正尝试添加JS使其看起来更漂亮。 Example: 例:

<div id="sidebar">
  <div class="steps online">
<div class="steps-hover online-hover"></div>    
  </div>
</div>

And the CSS 和CSS

.steps{
float: left;
margin: 0;
padding: 0;
display: block;
width: 240px;
text-align: center;
height: 234px;
cursor: pointer;
 } 
.online{
background: url("images/applyonline.jpg") top left no-repeat;
}
.online-hover{
background: url("images/online-hover.jpg") top left no-repeat;
}
.steps-hover{
display: none;
}
div.online:hover div.online-hover {
margin: 0;
padding: 0;
width: 960px;
text-align: center;
height: 180px;
position: absolute; 
top: 234px;
display: block;
left: 0;
z-index: 3;
right: 0px;
cursor: auto;
}

And from following examples in other posts, I attempted the following JS 从其他帖子中的以下示例中,我尝试了以下JS

<script type='text/javascript'>
$(document).ready(function()
{    
$("div.online").hover(
  function () {
    $("div.online-hover").fadeIn('slow');
  }, 
  function () {
    $("div.online-hover").fadeOut('slow');
  }
);
});​

</script>

But it's not working. 但这不起作用。 What did I do wrong? 我做错了什么? I'm so not good at JS, so feel free to dummy it down for me! 我对JS不太擅长,所以请随时为我加油!

You have an illegal char at the end of document ready closing braces remove it. 您在文档末尾有一个非法字符,准备用大括号将其删除。 It is better seen in fiddle . 最好在小提琴中看。

$(document).ready(function()
{    
$("div.online").hover(
  function () {
    $("div.online-hover").stop(true, true, true).fadeIn('slow');
  }, 
  function () {
    $("div.online-hover").stop(true, true, true).fadeOut('slow');
  }
);
});​ //<-- here

Also use .stop() to prevent any previous queued up animation from running and changing the normal behavior when you make frequent hovers. 还可以使用.stop()来防止任何先前排队的动画在频繁悬停时运行并更改正常行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM