时间:2021-07-01 10:21:17 帮助过:363人阅读
添加按钮那个简单,按钮函数中改变需要滚动的部件位置就是了,用jQuery等库还带滚动特效。
纵向横向这是客户端操作系统的事,配置鼠标属性就是了,如果有的话。
(function() { var stepSize = 200, //每滚动一格鼠标,移动多少距离 doc = document.documentElement, body = document.body, docWidth = doc.clientWidth, scrollLeft = 0;//添加mousewheel事件if (document.addEventListener) { document.addEventListener('mousewheel', scroll, false);} else { document.attachEvent('onmousewheel',scroll) //针对老ie浏览器}//处理mousewheel事件的信息function scroll (event) { var direction = event.wheelDelta; //保证滚动到头的时候不再调用update函数 if (scrollLeft <= 0 && direction > 0) { return; } if (scrollLeft >= docWidth && direction < 0) { return; } //根据鼠标滚动的方向确定是往左还是往右移动 var distance = direction > 0? -stepSize : stepSize; update(distance);}//滚动function update (distance) { scrollLeft += distance; doc.scrollLeft = scrollLeft; body.scrollLeft = scrollLeft; //针对webkit浏览器} })();
JavaScript code?123456789101112131415161718192021222324252627282930313233343536(function() { var stepSize = 200, //每滚动一格鼠标,移动多少距离 doc = document.documentElement, body = documen……
(function() {var stepSize = 200, //每滚动一格鼠标,移动多少距离 doc = document.documentElement, body = document.body, docWidth = doc.clientWidth, scrollLeft = -1, ready = false;//添加鼠标滚轮事件if (document.addEventListener) { document.addEventListener('mousewheel', scroll, false); document.addEventListener('DOMMouseScroll', scroll, false); //针对firefox} else { document.attachEvent('onmousewheel',scroll) //针对老ie浏览器}//处理mousewheel事件的信息function scroll (event) { //第一次滚动需要获取当前滚动位置 if (!ready) { scrollLeft = doc.scrollLeft + body.scrollLeft; ready = true; } //firefox用detail反映滚动方向,而且方向和其他浏览器相反。其他浏览器用wheelDelta var direction = event.wheelDelta || -event.detail; //保证滚动到头的时候不再调用update函数 if (scrollLeft <= 0 && direction > 0) { return; } if (scrollLeft >= docWidth && direction < 0) { return; } //根据鼠标滚动的方向确定是往左还是往右移动 var distance = direction > 0? -stepSize : stepSize; update(distance);}//滚动function update (distance) { scrollLeft += distance; doc.scrollLeft = scrollLeft; body.scrollLeft = scrollLeft; //针对webkit浏览器} })();
html, body { height: 100%;}
能搜索到很多解决方案,你可以参考下
(function() {var stepSize = 200, //每滚动一格鼠标,移动多少距离 doc = document.documentElement, body = document.body, docWidth = doc.clientWidth, scrollLeft = -1, ready = false;//添加鼠标滚轮事件if (document.addEventListener) { document.addEventListener('mousewheel', scroll, false); document.addEventListener('DOMMouseScroll', scroll, false); //针对firefox} else { document.attachEvent('onmousewheel',scroll) //针对老ie浏览器}//处理mousewheel事件的信息function scroll (event) { //第一次滚动需要获取当前滚动位置 if (!ready) { scrollLeft = doc.scrollLeft + body.scrollLeft; ready = true; } //firefox用detail反映滚动方向,而且方向和其他浏览器相反。其他浏览器用wheelDelta var direction = event.wheelDelta || -event.detail; //保证滚动到头的时候不再调用update函数 if (scrollLeft <= 0 && direction > 0) { return; } if (scrollLeft >= docWidth && direction < 0) { return; } //根据鼠标滚动的方向确定是往左还是往右移动 var distance = direction > 0? -stepSize : stepSize; update(distance);}//滚动function update (distance) { scrollLeft += distance; doc.scrollLeft = scrollLeft; body.scrollLeft = scrollLeft; //针对webkit浏览器} })();
html, body { height: 100%;}