文章源自:http://viralpatel.net/blogs/javascript-mouse-scroll-event-down-example/
Mouse Scroll Event Up/Down Example in JavaScript
The other day I was working on an App that required Google Map like functionality where Mouse Scroll event on an object triggered some action in JavaScript.
Handling Mouse Wheel in JavaScript is quite simple. Most of the browsers support Mouse Scroll Event in one or other way. Mozilla provides window.addEventListener
method that can be used to hook a handler for mouse scroll event. Internet Explorer and Opera on the other hand provides document.onmousewheel
handler to hook the mouse event.
Source Code
Let us see an example for catching mouse scroll wheel event in JavaScript. In our example we will have a small DIV that moves up and down on scroll of mouse wheel. Following is the source code of our example:
<html> <head> <title>Mouse Scroll Wheel example in JavaScript - ViralPatel.net</title> <style> #scroll { width: 250px; height: 50px; border: 2px solid black; background-color: lightyellow; top: 100px; left: 50px; position:absolute; } </style> <script language="javascript"> window.onload = function() { //adding the event listerner for Mozilla if(window.addEventListener) document.addEventListener('DOMMouseScroll', moveObject, false); //for IE/OPERA etc document.onmousewheel = moveObject; } function moveObject(event) { var delta = 0; if (!event) event = window.event; // normalize the delta if (event.wheelDelta) { // IE and Opera delta = event.wheelDelta / 60; } else if (event.detail) { // W3C delta = -event.detail / 2; } var currPos=document.getElementById('scroll').offsetTop; //calculating the next position of the object currPos=parseInt(currPos)-(delta*10); //moving the position of the object document.getElementById('scroll').style.top = currPos+"px"; document.getElementById('scroll').innerHTML = event.wheelDelta + ":" + event.detail; } </script> </head> <body> Scroll mouse wheel to move this DIV up and down. <div id="scroll">Dancing Div</div> </body> </html>
相关推荐
Alt+PgUp / PgDn Scroll page up/down Ctrl+Shift+[ Fold (collapse) region Ctrl+Shift+] Unfold (uncollapse) region Ctrl+K Ctrl+[ Fold (collapse) all subregions Ctrl+K Ctrl+] Unfold (uncollapse) all ...
《mousescroll.js网页滚动条插件:jQuery实现与应用详解》 在网页设计中,滚动条作为用户浏览内容的重要交互元素,其设计和功能的优化往往能提升用户体验。"mousescroll.js"是一款基于jQuery库的网页滚动条插件,它...
《jQuery+mousescroll.js自定义美化div滚动条插件详解》 在Web开发中,滚动条虽然看似不起眼,但其设计与交互往往能够显著影响用户体验。传统的浏览器默认滚动条样式单调,不符合现代网页设计的审美需求。jQuery库...
mousescroll.js是一款专为网页设计的滚动条插件,它基于JavaScript库jQuery构建,旨在提供更美观、交互性更强的滚动体验。这款插件的突出特点是其高度可定制化和易用性,允许开发者在同一个页面上多次应用,以满足...
而"js"文件夹则可能包含了插件的源代码和其他必要的JavaScript文件,如jQuery库和mousescroll.js脚本。 总结起来,"jQuery+mousescroll.js自定义美化div滚动条插件"是一个强大且灵活的工具,它利用jQuery的便利性和...
VBA-UserForm-MouseScroll.jpg
**mousescroll.js网页滚动条插件**是一款专为网页设计和开发人员打造的工具,主要用于增强网页的滚动体验。这款插件基于流行的JavaScript库jQuery构建,利用jQuery的高效性能和广泛兼容性,使得在各种浏览器环境中都...
本主题聚焦于“jquery scroll down”功能,即利用jQuery实现当用户鼠标向下滚动时,页面上的图片或其他元素能随滚动条缓缓移动的效果。这种效果常常用于创建动态和引人入胜的用户体验,特别是在长滚动或单页应用的...
npm install @appleple/scroll-up --save 通过纱线 yarn add @appleple/scroll-up 通过cdn < script src =" https://unpkg.com/@appleple/scroll-up@1.0.0/js/scroll-hint.js " > </ script > 用法 ...
3.Added: the mouse wheel scroll-up/down action to zoom in/out content in the HTML editor 4.Added: the 'Format brush' feature which enables to handily apply text style from current selection to another...
- Picasa-like smooth scrolling- Mouse wheel, middle mouse and keyboard support- Arrow keys, PgUp/PgDown, Spacebar, Home/End- Customizable step sizes, frames per second and more...- Works with ...
vue.js 的无限滚动指令。...new Vue({ directives: {infiniteScroll}})或者在 ES2015 中// register globallyimport infiniteScroll from 'vue-infinite-scroll'Vue.use(infiniteScroll)// or for a single i
* @description An ListView support (a) Pull down to refresh, (b) Pull up to load more. * Implement IXListViewListener, and see stopRefresh() / stopLoadMore(). */ package ...
为了解决这个问题,JavaScript引入了平滑滚动(Smooth Scroll)效果,它可以让页面以平滑、流畅的方式滚动到目标位置,提升用户体验。本篇文章将详细介绍如何利用JavaScript实现这一特效。 **一、基本原理** 平滑...
smoothScroll.js 是极小的,符合标准的平滑滚动脚本,无依赖,支持 Firefox, Chrome, IE10, Opera 和 Safari。使用:[removed][removed]示例:var smoothScroll = require('smoothscroll'); var exampleBtn = ...
Sortable is a <s>minimalist</s> JavaScript library for reorderable drag-and-drop lists. Demo: http://rubaxa.github.io/Sortable/ ## Features * Supports touch devices and [modern]...
### JavaScript Event.keyCode 大全解析 #### 一、概述 在JavaScript中,`event.keyCode`是用于捕获键盘事件中的键码的一种方式。虽然在现代Web开发中更推荐使用`event.key`或`event.code`来获取按键信息,但在...
用qt opengl 绘制多个立方体,并用鼠标控制左右上下旋转。用滚轮控制远近及距离(或者是投影距离)。
使用 Snipaste 提高您的...设置透明度(Ctrl+mouse scroll或Ctrl+ +/ -) 鼠标点击(X,并用于F4取消) 图像编辑 ( Space) 缩略图模式 ( Shift+ Double click) 隐藏 ( Double click) ... 颜色选择器 ( Alt) 拖放 分组
React向上滚动按钮React Component固定滚动到顶部按钮。...演示版安装npm install react-scroll-up-button用法默认按钮: import React from " react " ;import ScrollUpButton from " react-scroll-up-button " ; // A