2013-12-14 120 views
0

我正在這個網站上工作,叫http://martindue.dk/mmd3x9x/,我有這個滾動到頂部腳本,只是不會合並。我在很多其他網站上使用過該腳本,並且它工作正常,但是在這個特定的網站上,div#to-top不斷重新顯示,儘管我位於網站的頂部,爲什麼不它在頂部時會正確淡出?滾動到頂部的腳本 - div不會隱藏頂部

我的代碼看起來像這樣(的#到頂部的身體標記之後insertted在我的html):

的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/

+0

我看了一下你的小提琴代碼並沒有似乎看到描述的問題。當我滾動到頂部時,黑色小盒子漸漸消失,當我從頂部滾動時,它出現了。 –

+0

是的,這是奇怪的部分,因爲它可以很好地處理小提琴代碼,但不會出現在我的實際網站上。我猜想其他一些代碼可能會讓它起作用? – Dueify

+0

嘗試添加警報以查看滾動功能是否正常工作,警報(「滾動正在工作」);低於淡入。如果沒有警報彈出滾動,那麼你永遠不會去的功能,這是你的jQuery的問題(​​也許是一個錯過標籤的地方上面它搞亂他的代碼),如果它彈出你必須是無法找到div或東西 – Zach

回答

0

我知道這不是js,但我n這個情況下,你只使用衰落使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; 
} 

注:這是鉻看到這對跨瀏覽器兼容 http://www.w3schools.com/css/css3_transitions.asp