最近项目有个需求就是 在固定高度容器里放几十条的数据内容。不想使用滚动条,要用鼠标上下拖动时进行内容的滚动。花了点时间,写了个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