如何使用jQuery來使scrollLeft動畫化?


要使用 jQuery 使 scrollLeft 動畫化,請將 animate() 方法與 scrollLeft 聯合使用。

示例

你可以嘗試執行以下程式碼,瞭解如何使用 jQuery 使 scrollLeft 動畫化

線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('#scrollme').click(function() {
    $('html,body').animate({
        scrollLeft: $('#demo').css('left')
    }, 500, function() {

        $('html, body').animate({
            scrollLeft: 0
        }, 500);

    });
});
});
</script>
<style>
#demo {
    width: 100px;
    height: 100px;
    background: green;
    position: relative;
    left: 800px;
}
</style>
</head>
<body>
<p><button id="scrollme">Click to scroll left</button></p>

<div id="demo"></div>

</body>
</html>

更新於:2019-12-17

2K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲取認證

開始
廣告
© . All rights reserved.