gpt4 book ai didi

javascript - 使用鼠标悬停从左到右、从右到左滚动

转载 作者:行者123 更新时间:2023-12-01 03:57:53 24 4
gpt4 key购买 nike

我尝试使用 jQuery (jquery-1.7.1.min.js) 在 mouseover 上滚动,但无法滚动。下面是我的代码。

var ele = $('html,body');
var speed = 1,
scroll = 3,
scrolling;

$('#up').mouseenter(function() {
//scroll the element up
scrolling = window.setInterval(function() {
ele.scrollTop(ele.scrollTop() - scroll);
}, speed);
});

$('#down').mouseenter(function() {
//scroll the element down
scrolling = window.setInterval(function() {
ele.scrollTop(ele.scrollTop() + scroll);
}, speed);
});

$('#up, #down').bind({
click: function(e) {
//stop scrolling
if (scrolling) {
//prevents the default click action
e.preventDefault();
}
},
mouseleave: function() {
if (scrolling) {
window.clearInterval(scrolling);
scrolling = false;
}
}
});
.control {
width: 100%;
position: fixed;
text-align: center
}

#up.control {
position: fixed;
top: 0
}

#down.control {
position: fixed;
top: 20
}


/* no needed: */

#scroll {
overflow-x: scroll;
width: 500px;
white-space: nowrap;
overflow: hidden!imprtant;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="scroll">
Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content
here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here
and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and
more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more
content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content
here
</div>

<a href="#" id="up" class="control">left</a>
<a href="#" id="down" class="control">right</a>

onmouse 位于左侧和右侧,需要从左到右、从左到右滚动文本,但使用 jQuery 无法正常工作。

最佳答案

您的代码中存在一些问题:

  • ele = $('html,body');应该是您想要滚动的元素,而不是文档 <body><html> ,例如您的情况是 <div id="scroll">...
  • 您必须使用.scrollLeft()不是scrollTop()因为您是左右滚动,而不是上下滚动。

我相信这就是你想要的

var ele = $('#scroll');
var speed = 10,
scroll = 3,
scrolling;


$('#up').mouseenter(function() {
//scroll the element up
scrolling = window.setInterval(function() {
scroll += 0.5;
ele.scrollLeft(ele.scrollLeft() - scroll);
}, speed);

}).mouseleave(function(){
window.clearInterval(scrolling);
scroll = 3;
})

$('#down').mouseenter(function() {
//scroll the element down
scrolling = window.setInterval(function() {
scroll += 0.5;
ele.scrollLeft(ele.scrollLeft() + scroll);
}, speed);
})
.mouseleave(function(){
window.clearInterval(scrolling);
scroll = 3;
})
.control {
width: 100%;
position: fixed;
text-align: center
}

#up.control {
position: fixed;
top: 0
}

#down.control {
position: fixed;
top: 20
}


/* no needed: */

#scroll {
overflow-x: scroll;
width: 500px;
white-space: nowrap;
overflow: hidden!imprtant;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="scroll">
Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content
here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here
and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and
more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more
content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content
here
</div>

<a href="#" id="up" class="control">left</a>
<a href="#" id="down" class="control">right</a>

关于javascript - 使用鼠标悬停从左到右、从右到左滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60770100/

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