运用onmousewheel事件 写的幻灯片demo
2015-02-15 14:17
375 查看
使用鼠标的滚轮的可以便利了浏览网页,但是当使用滚轮作为控制器时,做一些网页幻灯片形式的展示时,我们要对鼠标的滚轮事件做一些自定义的调整。废话不多说先放上代码
(本段代码只在chrome跑过)
这里我们重新定义了事件onmousewheel。
其中我通过HTML DOM方式添加事件,
当然由于FireFox在mousewheel事件上与其他浏览的不同,可以通过一下方式对不同浏览器进行通配
接下来我们重点分析 自定义函数gundong()。(代码里出现的console.log(……);只是为了测试不同代码的执行情况)。
onmousewheel中一个重要的属性 wheelDelta 这是判断 不同的滚动的方向,虽然在不同的浏览器中,该属性的值不同,但是一致的是相同方向上 其返回的值 的正负性相同。
利用这一性质,我们可以开始写不同的情况
向下滚的条件,1.wheelDelta值小于0,2.页面不超出一定滚动范围
满足条件,则页面向下移一个屏幕高度的位置,即移一个页面。
向上的滚的条件事件 同上。细节略有变化。
接下来是增加用户体验的一个效果
用户滚动齿轮的一个齿就执行一次上面的代码,这样稍动一下滚轮就会出现移动完所有的页面的情况,所以在执行完一次 一次翻页,就注销当前的事件的功能,并设定一个时间重新获得功能,来实现,不管用户滚多少齿轮,一次只翻一页的效果。
读者可以将该片段代码从其中删除,来对比体会一下。
实现上面代码的一些css样式还要有些注意的地方
消除了 页面的滚动条,让其页面的滚动完全自定义
加入滚动时的动画过渡效果
(本段代码只在chrome跑过)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>幻灯片</title> <style> html,body{margin:0px;padding:0px;position:relative;} body{margin:0px;padding:0px;overflow:hidden;position:relative;} #box{position:absolute;top:0px;left:0px;z-index: -1;-webkit-transition-duration: 1s;} #b{position:absolute;right:30px;top:300px;z-index:10;} .cricle{height:10px;width:10px;border-radius: 5px;border:1px solid black;margin:10px 0px 10px 0px;} #c{width:12px;height:12px;border-radius: 5px;background-color: black;position:absolute;top:10px;left:0px;-webkit-transition-duration: 1s;} .d{position: relative;} </style> <script type="text/javascript"> window.onload = function(){ document.getElementsByTagName("body")[0].style.width = window.innerWidth + 'px'; document.getElementsByTagName("body")[0].style.height = window.innerHeight + 'px'; var a = document.getElementsByClassName("a"); var i = 0; while(a[i]){ a[i].style.width = window.innerWidth + 'px'; a[i].style.height = window.innerHeight + 'px'; i++; } function gundong(ev){ console.log("运行gundong"); var ev = ev || window.event; console.log(ev.wheelDelta); console.log(document.getElementById("box").style.top); if(ev.wheelDelta < 0 && parseInt(document.getElementById("box").style.top) >= - window.innerHeight * 3){ console.log("向下"); document.getElementById("box").style.top = parseInt(document.getElementById("box").style.top) - window.innerHeight + ' 4000 px'; console.log(document.getElementById("box").style.top); document.getElementById("c").style.top = parseInt(document.getElementById("c").style.top) + 22 +'px'; } if(ev.wheelDelta > 0 && parseInt(document.getElementById("box").style.top) < 0){ console.log("向上"); document.getElementById("box").style.top = parseInt(document.getElementById("box").style.top) + window.innerHeight + 'px'; document.getElementById("c").style.top = parseInt(document.getElementById("c").style.top) - 22 +'px'; } window.onmousewheel = document.onmousewheel = function(){console.log("消除功能");}; window.setTimeout(function(){window.onmousewheel = document.onmousewheel = gundong;},800); } window.onmousewheel = document.onmousewheel = gundong; } </script> </head> <body> <div id="box" style="top:0px;"> <div class="a" style="background-color:green"></div> <div class="a" style="background-color:red"></div> <div class="a" style="background-color:blue"></div> <div class="a" style="background-color:gray"></div> <div class="a" style="background-color:yellow"></div> </div> <div id="b"> <div id="d"> <div class="cricle"></div> <div class="cricle"></div> <div class="cricle"></div> <div class="cricle"></div> <div class="cricle"></div> <div id="c" style="top:10px;"></div> </div> </div> </body> </html>
这里我们重新定义了事件onmousewheel。
其中我通过HTML DOM方式添加事件,
window.onmousewheel = document.onmousewheel = gundong;
当然由于FireFox在mousewheel事件上与其他浏览的不同,可以通过一下方式对不同浏览器进行通配
if(document.addEventListener){ document.addEventListener('DOMMouseScroll',gundong,false); } window.onmousewheel = document.onmousewheel = gundong;
接下来我们重点分析 自定义函数gundong()。(代码里出现的console.log(……);只是为了测试不同代码的执行情况)。
onmousewheel中一个重要的属性 wheelDelta 这是判断 不同的滚动的方向,虽然在不同的浏览器中,该属性的值不同,但是一致的是相同方向上 其返回的值 的正负性相同。
利用这一性质,我们可以开始写不同的情况
if(ev.wheelDelta < 0 && parseInt(document.getElementById("box").style.top) >= - window.innerHeight * 3){ console.log("向下"); document.getElementById("box").style.top = parseInt(document.getElementById("box").style.top) - window.innerHeight + 'px'; console.log(document.getElementById("box").style.top); document.getElementById("c").style.top = parseInt(document.getElementById("c").style.top) + 22 +'px'; }
向下滚的条件,1.wheelDelta值小于0,2.页面不超出一定滚动范围
满足条件,则页面向下移一个屏幕高度的位置,即移一个页面。
向上的滚的条件事件 同上。细节略有变化。
接下来是增加用户体验的一个效果
window.onmousewheel = document.onmousewheel = function(){console.log("消除功能");}; window.setTimeout(function(){window.onmousewheel = document.onmousewheel = gundong;},800);
用户滚动齿轮的一个齿就执行一次上面的代码,这样稍动一下滚轮就会出现移动完所有的页面的情况,所以在执行完一次 一次翻页,就注销当前的事件的功能,并设定一个时间重新获得功能,来实现,不管用户滚多少齿轮,一次只翻一页的效果。
读者可以将该片段代码从其中删除,来对比体会一下。
实现上面代码的一些css样式还要有些注意的地方
body{overflow:hidden;} #box{-webkit-transition-duration: 1s;}
消除了 页面的滚动条,让其页面的滚动完全自定义
加入滚动时的动画过渡效果
相关文章推荐
- 如何运用onunload事件判断浏览器是刷新还是关闭窗口
- swt tray demo:右键菜单,单击隐藏,所有事件的例子
- 运用xlib进行事件响应(X11 API)的小例子
- 捕鱼达人(炮筒跟着鼠标旋转,委托事件的运用)
- 原生JS的事件监听写法DEMO
- GridView RowCommand事件操作Demo Code
- 运用Ntop监控网络流量(视频Demo)
- Vue.js移动端左滑demo和JS事件监听手机屏幕触摸事件 Touch
- unity游戏开发之自定义事件测试demo
- Android圆盘菜单(修改了网络上现有的demo总是显示第一个的bug,以及增加了点击事件)
- 一个canvas 事件demo
- 一只自动化测试小白的学习记录——Python+Selenium+Sublime 运用API、元素定位、元素操作方法、鼠标键盘事件实现入门Web自动化测试
- 慕课网js触发事件系列demo
- js点击事件demo
- 鼠标滑轮事件的差异onmousewheel和DOMMouseScroll
- 简单幻灯片 demo
- GoogleMap : [Google Map]GMark事件运用(GEvent)
- 单片机程序设计中运用事件驱动机制
- 高级事件的运用
- Qt 一个点击事件和改变按钮文字的demo