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

此滚动条仅支持竖向(Y轴)

一、Css

 /*这里是让用户鼠标在里面不能选中文字,避免拖动的时候出错*/
body { -moz-user-select: none; /*火狐*/ -webkit-user-select: none; /*webkit浏览器*/ -ms-user-select: none; /*IE10*/ -khtml-user-select: none; /*早期浏览器*/ user-select: none; } /*滚动文本及滚动条大框*/
.scroll_con { width: 500px; height: 500px; background-color: #5c0af7; position: relative; overflow-y: hidden; } /*滚动文本*/
.scroll_text { width: 480px; font-size: 14px; word-break: break-word; color: #ffffff; position: absolute; left:; top:; } /*滚动条整体框*/
.scroll { height: 500px; background-color: #2e03c4; position: absolute; left: 486px; top:; }
.scroll,.scroll .scroll_cen, .scroll .scroll_up, .scroll .scroll_down { width: 14px; }
/*滚轮上下按钮*/
.scroll .scroll_up, .scroll .scroll_down { height: 20px; line-height: 20px; background-color: #7263f8; color: #ffffff; font-size: 14px; font-weight: bold; text-align: center; }
.scroll .scroll_up:hover, .scroll .scroll_down:hover { background-color: #9da2f8; cursor: pointer; }
/*滚动滚动轨道*/
.scroll .scroll_cen { height: 460px; background-color: #2e03c4; position: relative; }
.scroll .scroll_cen .scroll_button { width: 12px; margin: 0px 1px; background-color: #7263f8; border-radius: 5px; position: absolute; cursor: pointer; }

二、Html

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta charset="utf-8" />
<script src="/jquery-1.10.2.js"></script>
<script src="/mousewheel.js"></script>
<script src="/scroll.js"></script>
</head> <body>
<div class="scroll_con">
<div class="scroll_text">
你需要放在滚动条可滑动区域的文本
</div>
</div> <script type="text/javascript">
//滚动条方法调用,括号里的数字为滑动速度
$(".scroll_con").LeonaScroll(50);
</script> </body>
</html>

三、Js

 $.fn.extend({
LeonaScroll: function (speed) {
return this.each(function () {
var elem = $(this);
var celem = $(this).find(".scroll_text");
var index = $(this).index();
//添加滚动条
scrollHTML = "";
scrollHTML += "<div class='scroll_up leonaup" + index + "'>∧</div>";
scrollHTML += "<div class='scroll_cen leonacen" + index + "'><div class='scroll_button leonabutton" + index + "'></div></div>";
scrollHTML += "<div class='scroll_down leonadown" + index + "'>∨</div>";
$(elem).append("<div class='scroll leonas" + index + "'> " + scrollHTML + "</div>"); var text_hidden = $(elem).height(),
text_show = $(celem).height(),
scroll_b = $(".leonabutton" + index + ""),
text_p = text_hidden / text_show,
bH_max = $(".leonas" + index + " .leonacen" + index + "").height(),
bH = text_p * bH_max;
if (text_p >= 1) $(".leonas" + index + "").css("display", "none"); else { $(".leonas" + index + "").css("display", "block"); scroll_b.css("height", bH + "px"); } //鼠标拖动div事件
var needMove = false, mouseY = 0;
scroll_b.mousedown(function (event) { needMove = true; var bH_Top = scroll_b.position().top; mouseY = event.pageY - bH_Top; });
$(document).mouseup(function (event) { needMove = false; });
$(document).mousemove(function (event) {
if (needMove) {
var sMouseY = event.pageY, bH_Top = sMouseY - mouseY, textY = bH_Top / bH_max * text_show;
if (bH_Top <= 0) {
scroll_b.css("top", 0);
$(celem).css("top", 0);
return;
}
if (bH_Top >= bH_max - bH) {
scroll_b.css("top", bH_max - bH);
$(celem).css("top", text_hidden - text_show);
return;
}
scroll_b.css("top", bH_Top); $(celem).css("top", -textY);
}
return;
}); function goGun(direction, timer) {
bH_Top = scroll_b.position().top;
var h = 0; h += speed; //调节滑动速度
if (direction == 1) { //up
var Toping = bH_Top - h;
if (bH_Top <= 0 || Toping <= 0) {
scroll_b.css("top", 0);
$(celem).css("top", 0);
if (timer == 2) clearInterval(goThread); //need timer
return;
}
scroll_b.css("top", bH_Top - h);
}
if (direction == -1) { //down
var Downing = bH_Top + h;
if (bH_Top >= bH_max - bH || Downing >= bH_max - bH) {
scroll_b.css("top", bH_max - bH);
$(celem).css("top", text_hidden - text_show);
if (timer == 2) clearInterval(goThread); //need timer
return;
}
scroll_b.css("top", bH_Top + h);
}
var textY = bH_Top / bH_max * text_show;
$(celem).css("top", -textY);
} //上下微调按钮事件
function minTiao(minTB, d, t) {
var goThread = "";
minTB.mouseup(function () { clearInterval(goThread); });
minTB.mousedown(function () {
clearInterval(goThread);
goThread = setInterval(function () { goGun(d, t); }, 300);
});
minTB.click(function () { goGun(d); });
}
minTiao($(".leonaup" + index + ""), 1, 2);
minTiao($(".leonadown" + index + ""), -1, 2); //滚轮事件
$(elem).bind("mousewheel", function (event, delta, deltaX, deltaY) {
if (delta == 1) {
goGun(1, 0);
if (scroll_b.position().top != 0)
return false;
} if (delta == -1) {
goGun(-1, 0);
if (Math.ceil(scroll_b.position().top) != Math.ceil(bH_max - bH))
return false;
}
});
});
}
});

四、插件下载链接:http://share.weiyun.com/2907af31e6427b83f7394014a62ca483 (密码:WBgr)【leonaScroll-1.0版本】

(此版本仅供学习,上有不足之处,敬请期待最新版本)

上一篇:Qt 学习之路 :Qt 线程相关类


下一篇:kali linux安装教程及VMware Tool工具的安装