滚动到顶部脚本 - div 在顶部时不会隐藏



我正在开发一个名为http://martindue.dk/mmd3x9x/我有一个滚动到顶部的脚本,它不会合并。我在许多其他网站上都使用过这个脚本,它运行得很好,但在这个特定的网站上,div#to-top不断出现,即使我在网站的最顶端,为什么它在顶端时不会正确地淡出?

我的代码看起来是这样的(#to-top插入在我的html中的body标记之后):

Javascript

jQuery(document).ready(function() {
    $toTop = jQuery("#to-top");
    $toTop.hide();
    jQuery(window).scroll(function() {
        if(jQuery(this).scrollTop() != 0) {
            $toTop.fadeIn();  
        } else {
            $toTop.fadeOut();
        }    
    });
    $toTop.click(function() {
        jQuery("body, html").animate({ scrollTop : 0 }, 500);
        return false;
    });
});

CSS

#to-top {
    background: url("img/to-top.png") center top no-repeat;
    background-size: 100%;
    width: 50px;
    height: 50px;
    position: fixed;
    bottom: 60px;
    right: 60px;
    cursor: pointer;
    /*display:none;*/
    /*opacity: 0.0;*/
}

我创造了这把小提琴,在这里它工作得很好:http://jsfiddle.net/2Rubp/

我知道它不是js,但在这种情况下,你只使用渐变,所以css可以做到这一点:

#to-top {
background: url("img/to-top.png") center top no-repeat;
background-size: 100%;
width: 50px;
height: 50px;
position: fixed;
bottom: 60px;
right: 60px;
cursor: pointer;
**-webkit-transition: all 0.5s linear;** 
display: none;
opacity: 0.0;
}

注意:这是针对chrome的,请参阅跨浏览器兼容性http://www.w3schools.com/css/css3_transitions.asp

最新更新