如何使用 jQuery 为 scrollLeft 制作动画?
jqueryweb developmentfront end technology
要使用 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>

