2017-02-23 19 views
0

我有我的index.php這是固定的按鈕,當用戶在頁面的底部的頂部工作。創建回到頂部按鈕使用jquery面臨的問題與代碼

<button id="fixed-btn"></button> 

和它的CSS是這樣的。

#fixed-btn{ 
position: fixed; 
    bottom: 50px; 
    right: 50px; 
    height: 30px; 
    width: 30px; 
    border-radius: 100%; 
    background-color: red; 
opacity: 0; 
} 
#fixed-btn.show{ 
    opacity: 1; 
} 

這樣做的我寫的jQuery

$(document).ready(function() 
{ 
    function testScroll(ev) 
    { 
    if(window.pageYOffset>400) 
    { 
     $('#fixed-btn').addClass('show'); 
    } 
    else 
    { 
     $('#fixed-btn').removeClass('show'); 
    } 
    } 
    window.onscroll=testScroll 
    $("#fixed-btn").click(function() 
    { 
    $('html, body').animate(
    { 
     scrollTop:0 
    }, 1500); 
    }); 

我不知道爲什麼它不working.button是不可見的。任何人都可以有想法,然後請分享。感謝提前:)

+2

你有沒有嘗試在css中z-index? @foram –

+1

它適合我。檢查https://jsfiddle.net/ou5rrww6/ –

+1

你想念這個'});'在最後 –

回答

0

我看到你的代碼,我認爲這將通過以下

#fixed-btn{ 
position: fixed; 
    bottom: 50px; 
    right: 50px; 
    height: 30px; 
    width: 30px; 
    border-radius: 100%; 
    background-color: red; 
z-index: 9999; 
opacity: 0; 
} 

給你只是在這個#fixed-btn{..}

添加z-index: 9999;如果仍然面臨着問題,那麼解決您的problem.replace你的CSS讓我知道。