gpt4 book ai didi

javascript - 当我只对一个 div 使用 jQuery scrollLeft 时,整个页面在顶部滚动

转载 作者:太空宇宙 更新时间:2023-11-04 09:12:19 24 4
gpt4 key购买 nike

我想要一个 div,我可以通过单击此 div 外部的链接来水平滚动它。 (一个用于向左滚动,一个用于向右滚动)。

我使用了 jQuery animate 和 scrollLeft。一切看起来都不错,但唯一的问题是:当我单击链接以使用 div 进行滚动时,整个页面也在顶部滚动。我在这里找到了一些类似的主题,但没有解决方案可以应用于我的问题(或者我可以应用于我的问题)。

请体谅,这是我的第一个问题。 :-)

谢谢

function move_right() {
var current = $('.thumbnail-scroll').scrollLeft();
var maximal = document.getElementById("thumbnail-scroll").scrollWidth;
var visible = document.getElementById("thumbnail-scroll").offsetWidth;
var move;

if ((current + visible) < maximal) {
$('.thumbnail-scroll').animate({
scrollLeft: '+=' + 470
}, 1000);
}
}

function move_left() {
var current = $('.thumbnail-scroll').scrollLeft();

if (current > 0) {
$('.thumbnail-scroll').animate({
scrollLeft: '-=' + 470
}, 1000);
}
}
.thumbnail-scroll  {
height: 150px;
overflow-x: auto;
white-space: nowrap;
overflow:hidden;
background-color: #cc33ff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style=" background-color: #6666ff; line-height: 230px;">
<div> some content </div>
<div> some content </div>
<div> some content </div>
</div>
<div style=" display: flex; ">
<div class="move-left" style="width: 150px; line-height: 145px; padding-right: 10px;">
<a onclick="move_left()" href="#">
<span> LEFT </span>
</a>
</div>
<div class="thumbnail-scroll" id="thumbnail-scroll">
<span> some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content </span>
</div>
<div class="move-right" style="width: 150px; line-height: 145px; padding-left: 10px;">
<a href="#" onclick="move_right()">
<span> RIGHT </span>
</a>
</div>
</div>

最佳答案

这是因为将 href 设置为 #。这是单击哈希链接时的正常行为(未在页面中找到)。

只需删除属性即可解决问题。 (另一种方法是通过 jQuery 绑定(bind)处理程序并对传递的事件使用 preventDefault())

function move_right() {
var current = $('.thumbnail-scroll').scrollLeft();
var maximal = document.getElementById("thumbnail-scroll").scrollWidth;
var visible = document.getElementById("thumbnail-scroll").offsetWidth;
var move;

if ((current + visible) < maximal) {


$('.thumbnail-scroll').animate({
scrollLeft: '+=' + 470
}, 1000);

}
}



function move_left() {
var current = $('.thumbnail-scroll').scrollLeft();


if (current > 0) {


$('.thumbnail-scroll').animate({
scrollLeft: '-=' + 470
}, 1000);

}
}
.thumbnail-scroll  {
height: 150px;
overflow-x: auto;
white-space: nowrap;
overflow:hidden;
background-color: #cc33ff;
}

a[onclick]{cursor:pointer;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style=" background-color: #6666ff; line-height: 230px;">
<div> some content </div>
<div> some content </div>
<div> some content </div>
</div>
<div style=" display: flex; ">
<div class="move-left" style="width: 150px; line-height: 145px; padding-right: 10px;">
<a onclick="move_left()">
<span> LEFT </span>
</a>
</div>
<div class="thumbnail-scroll" id="thumbnail-scroll">
<span> some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content some content </span>
</div>
<div class="move-right" style="width: 150px; line-height: 145px; padding-left: 10px;">
<a onclick="move_right()">
<span> RIGHT </span>
</a>
</div>
</div>

关于javascript - 当我只对一个 div 使用 jQuery scrollLeft 时,整个页面在顶部滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42034983/

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