gpt4 book ai didi

javascript - 鼠标移动时对scrollLeft和scrollTop进行动画处理

转载 作者:行者123 更新时间:2023-12-02 16:08:14 26 4
gpt4 key购买 nike

我想根据鼠标光标的移动来滚动 HTML 页面,以便当向任何方向(X 和 Y)移动鼠标光标时,页面的滚动条随之移动,从而滚动页面。我还希望滚动有缓动,以便移动鼠标时有轻微的延迟。

我有一个工作原型(prototype)(没有缓动): https://jsfiddle.net/ssq7cda4/12/

$(document).mousemove(function (e) {
var winW = $(window).width(),
docW = $(this).width() - winW,
i = docW / winW, //increment value
x = (e.pageX - $(window).scrollLeft()) * i;

//$(window).scrollLeft(x);
//$("html, body").animate({scrollLeft:x}, 500, 'swing');
$("html, body").scrollLeft(x);
});

$(document).mousemove(function (e) {
var winW = $(window).height(),
docW = $(this).height() - winW,
i = docW / winW, //increment value
x = (e.pageY - $(window).scrollTop()) * i;

//$("html, body").animate({scrollTop:x}, 500, 'swing');
$(window).scrollTop(x);
});

唯一的问题是我还没有找到一种方法来添加缓动滚动。使用 .animate 会使滚动变得锯齿状且奇怪。我应该如何根据鼠标移动通过缓动来设置滚动动画?

最佳答案

这对你不起作用吗?

var win=$(window),doc=$(document);
var winWidth=win.width(),winHeight=win.height();
var docWidth=doc.width()-winWidth,docHeight=doc.height()-winHeight;
var increment=0,x=0,y=0;
doc.on('mousemove',function(e){
increment=docWidth/winWidth;
x=(e.pageX-win.scrollLeft())*increment;
increment=docHeight/winHeight;
y=(e.pageY-win.scrollTop())*increment;
$('html,body').stop().animate({scrollLeft:x,scrollTop:y});
});
#canvas { width: 3000px; height: 2000px; background: #ededed; }

.content {
position: absolute;
border: 3px solid pink;
width: 300px;
height: 300px;
padding: 30px;
}

#island1 { top: 200px; left: 400px; }
#island2 { top: 300px; left: 1700px; }
#island3 { top: 700px; left: 1400px; }
#island4 { top: 1200px; left: 1900px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="canvas">
<div class="content" id="island1"><p>Hello world!</p></div>
<div class="content" id="island2"><p>Hello world!</p></div>
<div class="content" id="island3"><p>Hello world!</p></div>
<div class="content" id="island4"><p>Hello world!</p></div>
</div>

希望它能在某种程度上有所帮助。

更新:

试试这个:

window.requestAnimFrame = (function () {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

var win=$(window),doc=$(document),canvas=$('#canvas');
var winWidth=win.width(),winHeight=win.height();
var docWidth=doc.width()-winWidth,docHeight=doc.height()-winHeight;
var increment=0,x=0,y=0,mouseX=0,mouseY=0,damping=10;

requestAnimFrame(render);

doc.on('mousemove',function(e){
// see [http://stackoverflow.com/a/23202637/3344111]
mouseX=e.pageX*(canvas.width()-winWidth)/winWidth;
mouseY=e.pageY*(canvas.height()-winHeight)/winHeight;
});

function render(){
requestAnimFrame(render);
// see [http://stackoverflow.com/a/4847893] & [http://stackoverflow.com/a/4315833]
x+=(-mouseX-x)/damping;
y+=(-mouseY-y)/damping;
canvas.css({'-webkit-transform':'translate('+x+'px,'+y+'px)'});
}
#canvas { width: 3000px; height: 2000px; background: #ededed; }

.content {
position: absolute;
border: 3px solid pink;
width: 300px;
height: 300px;
padding: 30px;
}

#island1 { top: 200px; left: 400px; }
#island2 { top: 300px; left: 1700px; }
#island3 { top: 700px; left: 1400px; }
#island4 { top: 1200px; left: 1900px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="canvas">
<div class="content" id="island1"><p>island1</p></div>
<div class="content" id="island2"><p>island2</p></div>
<div class="content" id="island3"><p>island3</p></div>
<div class="content" id="island4"><p>island4</p></div>
</div>

有帮助吗?

关于javascript - 鼠标移动时对scrollLeft和scrollTop进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30502002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com