自定义滚动条样式
程序员文章站
2024-03-26 13:24:17
...
html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 300px;
height: 500px;
border: 1px solid red;
margin: 100px;
position: relative;
overflow: hidden;
}
.content {
padding: 5px 18px 5px 5px;
position: absolute;
top: 0;
left: 0;
}
.scroll {
width: 18px;
height: 100%;
position: absolute;
top: 0;
right: 0;
background-color: #eee;
}
.bar {
height: 100px;
width: 100%;
position: absolute;
top: 0;
left: 0;
background-color: red;
border-radius: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="content" id="content">
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头
床前明月光啊,明月光,疑是地上霜啊,举头嘿嘿
</div><!--文字内容-->
<div id="scroll" class="scroll"><!--装滚动条的层-->
<div class="bar" id="bar"></div><!--滚动条-->
</div>
</div>
<script src="common.js"></script>
<script>
//获取需要的元素
//最外面的div
var box = my$("box");
//文字div
var content = my$("content");
//装滚动条的div---容器
var scroll = my$("scroll");
//滚动条
var bar = my$("bar");
//设置滚动条的高度
//滚动条的高/装滚动条的div的高=box的高/文字div的高
//滚动条的高=装滚动条的div的高*box的高/文字div的高
var height = scroll.offsetHeight * box.offsetHeight / content.offsetHeight;
bar.style.height = height + "px";
//移动滚动条
bar.onmousedown = function (e) {
var spaceY = e.clientY - bar.offsetTop;
document.onmousemove = function (e) {//移动事件
var y = e.clientY - spaceY;
y=y<0?0:y;//最小值
y=y>scroll.offsetHeight-bar.offsetHeight?scroll.offsetHeight-bar.offsetHeight:y;
bar.style.top = y + "px";
//设置鼠标移动的时候,文字不被选中
window.getSelection? window.getSelection().removeAllRanges():document.selection.empty();
//滚动条的移动距离/文字div的移动距离=滚动条最大的移动距离/文字div的最大移动距离
//文字div的移动距离=滚动条的移动距离*文字div的最大移动距离/滚动条最大的移动距离
var moveY=y*(content.offsetHeight-box.offsetHeight)/(scroll.offsetHeight-bar.offsetHeight);
//设置文字div的移动距离
content.style.marginTop=-moveY+"px";
};
};
document.onmouseup=function () {
//鼠标抬起的时候,把移动事件干掉
document.onmousemove=null;
};
</script>
</body>
</html>
js
//foreach的兼容代码
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(' this is null or not defined');
}
// 1. Let O be the result of calling toObject() passing the
// |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get() internal
// method of O with the argument "length".
// 3. Let len be toUint32(lenValue).
var len = O.length >>> 0;
// 4. If isCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
// 5. If thisArg was supplied, let T be thisArg; else let
// T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as
// the this value and argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
/**
* Created by Administrator on 2017-08-18.
*/
//格式化日期的代码
//根据id获取元素的代码
//innerText和textContent的兼容
//获取第一个子元素的兼容
//获取最后一个子元素的兼容
/**
* Created by Administrator on 2017/3/24.
*/
/**
* 格式化日期
* @param dt 日期对象
* @returns {string} 返回值是格式化的字符串日期
*/
function getDates(dt) {
var str = "";//存储时间的字符串
//获取年
var year = dt.getFullYear();
//获取月
var month = dt.getMonth() + 1;
//获取日
var day = dt.getDate();
//获取小时
var hour = dt.getHours();
//获取分钟
var min = dt.getMinutes();
//获取秒
var sec = dt.getSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hour = hour < 10 ? "0" + hour : hour;
min = min < 10 ? "0" + min : min;
sec = sec < 10 ? "0" + sec : sec;
str = year + "年" + month + "月" + day + "日 " + hour + ":" + min + ":" + sec;
return str;
}
/**
* 获取指定标签对象
* @param id 标签的id属性值
* @returns {Element}根据id属性值返回指定标签对象
*/
function my$(id) {
return document.getElementById(id);
}
/**
* 设置元素的文本内容
* @param element 任意元素
* @param text 任意文本内容
*/
function setInnerText(element, text) {
if (typeof(element.textContent) == "undefined") {
element.innerText = text;
} else {
element.textContent = text;
}
}
/**
* 获取元素的文本内容
* @param element 任意元素
* @returns {*} 任意元素中的文本内容
*/
function getInnerText(element) {
if (typeof(element.textContent) == "undefined") {
return element.innerText;
} else {
return element.textContent;
}
}
/**
* 获取父级元素中的第一个子元素
* @param element 父级元素
* @returns {*} 父级元素中的子级元素
*/
function getFirstElement(element) {
if (element.firstElementChild) {
return element.firstElementChild;
} else {
var node = element.firstChild;
while (node && node.nodeType != 1) {
node = node.nextSibling;
}
return node;
}
}
/**
* 获取父级元素中的最后一个子元素
* @param element 父级元素
* @returns {*} 最后一个子元素
*/
function getLastElement(element) {
if (element.lastElementChild) {
return element.lastElementChild;
} else {
var node = element.lastChild;
while (node && node.nodeType != 1) {
node = node.previousSibling;
}
return node;
}
}
/**
* 获取某个元素的前一个兄弟元素
* @param element 某个元素
* @returns {*} 前一个兄弟元素
*/
function getPreviousElement(element) {
if (element.previousElementSibling) {
return element.previousElementSibling
} else {
var node = element.previousSibling;
while (node && node.nodeType != 1) {
node = node.previousSibling;
}
return node;
}
}
/**
* 获取某个元素的后一个兄弟元素
* @param element 某个元素
* @returns {*} 后一个兄弟元素
*/
function getNextElement(element) {
if (element.nextElementSibling) {
return element.nextElementSibling
} else {
var node = element.nextSibling;
while (node && node.nodeType != 1) {
node = node.nextSibling;
}
return node;
}
}
/**
* 获取某个元素的所有兄弟元素
* @param element 某个元素
* @returns {Array} 兄弟元素
*/
function getSiblings(element) {
if (!element)return;
var elements = [];
var ele = element.previousSibling;
while (ele) {
if (ele.nodeType === 1) {
elements.push(ele);
}
ele = ele.previousSibling;
}
ele = element.nextSibling;
while (ele) {
if (ele.nodeType === 1) {
elements.push(ele);
}
ele = ele.nextSibling;
}
return elements;
}
/**
* 返回当前浏览器是什么类型的浏览器
*/
function userBrowser(){
var browserName=navigator.userAgent.toLowerCase();
if(/msie/i.test(browserName) && !/opera/.test(browserName)){
console.log("IE");
}else if(/firefox/i.test(browserName)){
console.log("Firefox");
}else if(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName)){
console.log("Chrome");
}else if(/opera/i.test(browserName)){
console.log("Opera");
}else if(/webkit/i.test(browserName) &&!(/chrome/i.test(browserName) && /webkit/i.test(browserName) && /mozilla/i.test(browserName))){
console.log("Safari");
}else{
console.log("不知道什么鬼!");
}
}
//为任意一个元素绑定事件:元素,事件类型,事件处理函数
function addEventListener(element,type,fn) {
if(element.addEventListener){
//支持
element.addEventListener(type,fn,false);
}else if(element.attachEvent){
element.attachEvent("on"+type,fn);
}else{
element["on"+type]=fn;
}
}
//为任意的一个元素解绑某个事件:元素,事件类型,事件处理函数
function removeEventListener(element,type,fn) {
if(element.removeEventListener){
element.removeEventListener(type,fn,false);
}else if(element.detachEvent){
element.detachEvent("on"+type,fn);
}else{
element["on"+type]=null;
}
}
/**
* 获取的是页面向上或者向左卷曲出去的距离的值,返回的是对象
* @returns {{top: (Number|number), left: (Number|number)}}
*/
function getScroll() {
return {
top: window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop || 0,
left: window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft||0
};
}
效果图
上一篇: Java常见应用-反射,RPC