use*_*764 9 navigation jquery horizontal-scrolling
基本上我有一个水平滚动内容的网站.在整个页面中水平滚动,而不仅仅是div.
我正在尝试实现onmousedown或悬停的左右箭头按钮(无论哪个都没关系),向左和向右滚动整个窗口(平滑).
这个网站完全符合我想要的小中心箭头:http://www.clholloway.co.za
有人能帮忙吗?干杯.
Jos*_*osh 14
我想通过一点点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>
Run Code Online (Sandbox Code Playgroud)
还有一些样式可以确保它会导致窗口滚动:
#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;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用样式,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;
}
}());
Run Code Online (Sandbox Code Playgroud)
完整的jsFiddle展示了这种方法:http://jsfiddle.net/jwcarroll/atAHh/