js卷轴滚动 鼠标拖动滚动。例子

博客展示了如何使用JavaScript创建鼠标拖动滚动的功能,适用于具有WebKit的移动设备,如Android和iPhone。通过引入scroll.js并调用Scroller构造函数即可实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近项目有个需求就是  在固定高度容器里放几十条的数据内容。不想使用滚动条,要用鼠标上下拖动时进行内容的滚动。花了点时间,写了个Demo。贴代码:

 

效果如下:(鼠标拖动了几个像素的图片)

代码:scroll.js

/* 可滚动卷轴对象
 * @description : 容器里拖动鼠标可实现内容滚动,兼容各类浏览器(ie6/7/8/firefox/safari/opera/chrome/)。
 *      using: new Scroller(@id|{id:@id,drag:false|true,scroll:true|false,scrollBase:@number}) 即可  id为你的容器对象或容器ID
 * @date  : 2011-07-12
 * @author  : limanzu753@163.com
 * history  : 
 *    2011-07-13  修改鼠标在容器外里还可以继续滚动 0.11 limanzu753@163.com  
 *    2011-07-20  增加鼠标滚动,动态配置滚动像素,动态配置是否支持鼠标滚动以及鼠标拽动滚动功能
 */
function Scroller(args) {
 if (this instanceof  Scroller == false) {return new Scroller(args);}
 if (typeof args === "string" ) {
  this.container = this.$(args); //容器对象 
  this.drag = true;
  this.scroll = true;
  this.scrollBase = 35;
 } else {
  this.container = this.$(args.id || args.container || args.obj); //容器对象 
  this.drag = args.drag == undefined ? true : args.drag;
  this.scroll = args.scroll == undefined ? true : args.scroll;
  this.scrollBase = args.scrollBase || 35;
 }
 this.y = 0;//mousedown时记录鼠标的纵坐标
 this.init();
}
Scroller.prototype = {
 $:function(id) {
  if (typeof id === "object") { return id; } 
  return do
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值