gpt4 book ai didi

jquery - 使用箭头按钮水平滚动窗口

转载 作者:行者123 更新时间:2023-12-03 22:25:31 25 4
gpt4 key购买 nike

基本上我有一个带有水平滚动内容的网站。因为整个页面水平滚动,而不仅仅是一个 div。

我正在尝试实现左右箭头按钮,这些按钮在鼠标按下或悬停时(哪个还不太重要),可以(平滑地)左右滚动整个窗口。

该网站通过中心小箭头几乎满足了我的要求:http://www.clholloway.co.za

谁能帮忙解决这个问题吗?干杯。

最佳答案

我认为用一点 jQuery 你就可以实现你想要的。基本思想是处理一些事件(onmouseenter、mousedown 等),然后您可以使用这些事件来启动滚动。

假设您有一些如下所示的标记:

<div id="parent">
<div class="contentBlock">1</div>
<div class="contentBlock">2</div>
<div class="contentBlock">3</div>
<div class="contentBlock">4</div>
<div class="contentBlock">5</div>
</div>
<span id="panLeft" class="panner" data-scroll-modifier='-1'>Left</span>

<span id="panRight" class="panner" data-scroll-modifier='1'>Right</span>

以及一些确保它会导致窗口滚动的样式:

#parent {
width:6000px;
}
.contentBlock {
font-size:10em;
text-align:center;
line-height:400px;
height:400px;
width:500px;
margin:10px;
border:1px solid black;
float:left;
}
.panner {
border:1px solid black;
display:block;
position:fixed;
width:50px;
height:50px;
top:45%;
}
.active{
color:red;
}
#panLeft {
left:0px;
}
#panRight {
right:0px;
}

您可以结合使用样式、setInterval 和 jQuery.scrollLeft() 来实现您想要的效果。

(function () {

var scrollHandle = 0,
scrollStep = 5,
parent = $(window);

//Start the scrolling process
$(".panner").on("mouseenter", function () {
var data = $(this).data('scrollModifier'),
direction = parseInt(data, 10);

$(this).addClass('active');

startScrolling(direction, scrollStep);
});

//Kill the scrolling
$(".panner").on("mouseleave", function () {
stopScrolling();
$(this).removeClass('active');
});

//Actual handling of the scrolling
function startScrolling(modifier, step) {
if (scrollHandle === 0) {
scrollHandle = setInterval(function () {
var newOffset = parent.scrollLeft() + (scrollStep * modifier);

parent.scrollLeft(newOffset);
}, 10);
}
}

function stopScrolling() {
clearInterval(scrollHandle);
scrollHandle = 0;
}

}());

完整的 jsFiddle 演示了这种方法:http://jsfiddle.net/jwcarroll/atAHh/

关于jquery - 使用箭头按钮水平滚动窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18087088/

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