在实际项目中,经常由于浏览器自带的滚动条样式太戳,而且在各个浏览器中显示不一样,所以我们不得不去实现自定义的滚动条,今天我就用最少的代码实现了一个自定义滚动条,代码量区区只有几十行,使用起来也非常方便。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>自定义滚动条</title>
<style type="text/css">
body,ul,dl,dd{ margin:0; padding:0; font-size:12px; font-family:'微软雅黑';}
.clear{ zoom:1}
.clear:after{ display:block; content:""; clear:both}
.box{margin:28px auto; width:164px; height:440px; background:#000; padding:8px;}
#wrap{ width:170px; height:386px; background:#1c1c1d; overflow:hidden; position:relative; }
#listbox{ float:left; width:160px; position:absolute; left:0; top:0; }
#scrollbox{ float:right; width:3px; height:386px; background: #333; position:relative;}
#scrollbar{ width:3px; height:30px; background:#cc3f2e; position:absolute; left:0; top:0;} li{ height:80px; margin-bottom:8px; list-style:none;}
.picbox,.div{ float:left;}
.picbox{ width:150px; position:relative; }
.picbox span{ position:absolute; left:0px; bottom:0px; width:90px; height:22px;}
dl{ width:139px; height:76px; color:#fff; padding:4px 0 0 10px;line-height:18px; position:absolute;left:0; top:0; opacity:0.2; filter:alpha(opacity=20); }
.div{width:149px; height:80px; position:relative; }
.mark{width:149px; height:80px; position:absolute; top:0; left:0; background:#fff; opacity:0.1; filter:alpha(opacity=10);} .nav{ width:160px; height:35px; font-family:'微软雅黑';background:#1c1c1d; padding-top:5px;}
.a,.b{ width:152px; height:24px; line-height:24px; background:#666; text-align:center; float:left; color:#fff; cursor:pointer}
.a{margin-right:5px; display:inline; background:#333; color:#666; } li.active .mark{opacity:0.2; filter:alpha(opacity=20);}
li.active dl{ opacity:0.7; filter:alpha(opacity=70);}
</style>
</head>
<body>
<div class="box">
<div class="nav">
<div class="a">猜你喜欢</div>
</div>
<div id="wrap">
<ul id="listbox">
<li class="active">
<div class="picbox">
<span class="a1"></span>
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
<span class="a2"></span>
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
<span class="a2"></span>
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
<span></span>
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
<li>
<div class="picbox">
</div>
<div class="div">
<div class="mark"></div>
<dl class="textbox">
<dt>飞狐外传</dt>
<dd>主演:方力申 周秀娜 谈莉娜 周秀娜施展媚术</dd>
<dd>史上身材最棒狐仙</dd>
</dl>
</div>
</li>
</ul>
<div id="scrollbox">
<div id="scrollbar"></div>
</div>
</div>
</div>
</body> <script src="tinyScroll.js"></script>
<script>
new tinyScroll({
wrapBox : document.getElementById('wrap'),
wrapList : document.getElementById('listbox'),
scrollBox :document.getElementById('scrollbox'),
scrollBar : document.getElementById('scrollbar')
})
</script>
</html>

然后引入滚动条插件:

/**
* javascript自定义滚动条(滚动条高度可以动态改变)
* @param opts
* 调用方式
* new tinyScroll({
wrapBox : document.getElementById('wrap'),
wrapList : document.getElementById('listbox'),
scrollBox :document.getElementById('scrollbox'),
scrollBar : document.getElementById('scrollbar')
})
*/
function tinyScroll(opts) {
this.wrapBox = opts.wrapBox;
this.wrapList = opts.wrapList;
this.scrollBox = opts.scrollBox;
this.scrollBar = opts.scrollBar;
this.scale = 0;
this.height = 0;
this.maxTop = 0;
this.ListMaxTop = 0;
this.top = 0; this.bindEvent();
}
tinyScroll.prototype = {
constructor: tinyScroll,
init: function() {
this.scale = this.wrapBox.clientHeight / this.wrapList.scrollHeight;
if (this.scale > 1) this.scale = 1;
this.height = this.scale * this.scrollBox.scrollHeight;
this.maxTop = this.scrollBox.scrollHeight - this.height;
this.ListMaxTop = this.wrapBox.clientHeight - this.wrapList.scrollHeight;
if (this.scale == 1) this.scrollBox.style.display = 'none';
this.scrollBar.style.height = this.height + 'px';
},
bindEvent: function() {
var that = this;
var mouseScroll = function(ev) {
ev = ev || event;
var fx = ev.wheelDelta || ev.detail;
var bDown = true;
if (ev.detail) {
bDown = fx > 0 ? true: false;
} else {
bDown = fx > 0 ? false: true;
}
if (bDown) {
that.top += 10;
} else {
that.top -= 10;
}
that.fnScroll();
if (ev.preventDefault) {
ev.preventDefault();
}
return false;
};
this.init();
this.scrollBar.onmousedown = function(ev) {
ev = ev || event;
var disY = ev.clientY - this.offsetTop;
document.onmousemove = function(ev) {
ev = ev || event;
that.top = ev.clientY - disY;
that.fnScroll();
};
document.onmouseup = function() {
document.onmouseup = document.onmousemove = null;
};
return false;
};
this.wrapBox.onmousewheel = mouseScroll;
if (this.wrapList.addEventListener) {
this.wrapList.addEventListener('DOMMouseScroll', mouseScroll, false);
}
},
fnScroll: function() {
if (this.top < 0) this.top = 0;
if (this.top > this.maxTop) this.top = this.maxTop;
var scale = this.top / this.maxTop;
var listTop = scale * this.ListMaxTop;
this.scrollBar.style.top = this.top + 'px';
this.wrapList.style.top = listTop + 'px';
}
};

将所贴代码复制到编辑器中进行预览吧,是不是感觉特酷!

这里的滚动条样式你想设计成那样都行,给予你充分的发挥空间,是不是感觉挺爽!

javascript自定义滚动条插件,几行代码的事儿的更多相关文章

  1. 分享一个开源的JavaScript统计图表库,40行代码实现专业统计图表

    提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...

  2. JavaScript模板引擎原理,几行代码的事儿

    一.前言 什么是模板引擎,说的简单点,就是一个字符串中有几个变量待定.比如: var tpl = 'Hei, my name is <%name%>, and I\'m <%age% ...

  3. JavaScript学习笔记- 自定义滚动条插件

    此滚动条仅支持竖向(Y轴) 一.Css /*这里是让用户鼠标在里面不能选中文字,避免拖动的时候出错*/ body { -moz-user-select: none; /*火狐*/ -webkit-us ...

  4. JavaScript-JQ实现自定义滚动条插件1.0

    此滚动条仅支持竖向(Y轴) 一.Css /*这里是让用户鼠标在里面不能选中文字,避免拖动的时候出错*/ body { -moz-user-select: none; /*火狐*/ -webkit-us ...

  5. Jquery自定义滚动条插件

    下载地址:http://files.cnblogs.com/files/LoveOrHate/jquery.nicescroll.min.js <script src="jquery. ...

  6. 自定义滚动条插件 mCustomScrollbar 使用介绍

    引用有心的学士笔记 http://www.wufangbo.com/mcustomscrollbar/ http://www.jianshu.com/p/550466260856 官网地址 http: ...

  7. jQuery自定义滚动条样式插件mCustomScrollbar

    如果你构建一个很有特色和创意的网页,那么肯定希望定义网页中的滚动条样式,这方面的 jQuery 插件比较不错的,有两个:jScrollPane 和 mCustomScrollbar. 关于 jScro ...

  8. 自定义滚动条 - mCustomScrollbar

    项目中需要使用自定义滚动条,但是试用了很多都功能不够全,今天发现一个比较全而且用法很简单的 -- mCustomScrollbar http://manos.malihu.gr/jquery-cust ...

  9. 如何创建一个自定义jQuery插件

    简介 jQuery 库是专为加快 JavaScript 开发速度而设计的.通过简化编写 JavaScript 的方式,减少代码量.使用 jQuery 库时,您可能会发现您经常为一些常用函数重写相同的代 ...

随机推荐

  1. BZOJ1070 [SCOI2007]修车

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  2. text-indent无效解决方案

    text-indent是用来字符缩进的. 1.text-indent所在的元素是行内元素而非块级元素.比如用在span,a等行内元素上.解决方案:在行内元素加上display:block; 或者把目标 ...

  3. [Android]Volley源码分析(一)

    一. 如何使用Volley? 1. 首先定义一个RequestManager类,用来在Android程序启动时对Volley进行初始化.RequestManager为单例类,因为只有在程序启动时调用, ...

  4. Linux 之 编译器 gcc/g++参数详解

    2016年12月9日16:48:53 ----------------------------- 内容目录: [介绍] gcc and g++分别是gnu的c & c++编译器 gcc/g++ ...

  5. JSP+JavaBean+Servlet+Oracle新增功能中对Date类型的字段的处理

    Oracle库中userinfo表borth字段是Date类型,age年纪字段是int类型.age字段要根据borth来自动计算 先说一下我遇到的问题: insert into的时候遇到日期转换类型错 ...

  6. Chrome控制台 JS调试的一些小技巧

    $ $_命令返回最近一次表达式执行的结果,功能跟按向上的方向键再回车是一样的,但它可以做为一个变量使用在你接下来的表达式中. $0~$4则代表了最近5个你选择过的DOM节点.在页面右击选择审查元素,然 ...

  7. Low Power Consumption Design --- MCU Attention

    20161008 note : I have a PCB board called 'A' where a piece of STM8L052C6 and a piece of CC1101 are ...

  8. css006 文本格式化

    css006 文本格式化 文本格式化:字体(font-family).颜色(color).字号(font-size). 行距(line-height).粗体(font-weight).斜体(font- ...

  9. linq 之 Distinct的使用

    public class Product { public string Name { get;set;} public int Code { get; set; } } class ProductC ...

  10. 面试题目——《剑指Offer》

    1.把一个字符串转换成整数——<剑指Offer>P29 2.求链表中的倒数第k个结点——<剑指Offer>P30 3.实现Singleton模式——<剑指Offer> ...