自定義滾動條——控制div的大小和透明度


<!DOCTYPE html>

<html>

 

<head>

<meta charset="UTF-8">

<title></title>

<style type="text/css">

#big {

height: 20px;

width: 600px;

background-color: gray;

position: relative;

margin: 100px auto;

}

 

#small {

height: 20px;

width: 20px;

background-color: red;

position: absolute;

left: 0;

top: 0;

}

 

#side {

width: 0px;

height: 0px;

background-color: green;

}

</style>

</head>

 

<body>

<div id="big">

<div id="small">

 

</div>

</div>

<div id="side">

 

</div>

</body>

<script type="text/javascript">

//實現通過滾動條來控制另一個div的寬高和透明度

//滾動條只能左右滾動

var big = document.getElementById("big");

var small = document.getElementById("small");

var side = document.getElementById("side");

var x = 0;

var y = 0;

small.onmousedown = function(ev) {

var oEvent = ev || event;

x = oEvent.clientX - small.offsetLeft;

document.onmousemove = function(ev) {

var oEvent = ev || event;

var Left = oEvent.clientX - x;

if(Left < 0) {

Left = 0;

} else if(Left > big.offsetWidth - small.offsetWidth) {

Left = big.offsetWidth - small.offsetWidth;

}

small.style.left = Left + "px";

//自定義一個變量scale

//表示滾動條滾動的距離和滾動條可以滾動的最大距離的的比

var scale = Left / (big.offsetWidth - small.offsetWidth);

document.title = scale;

//通過滑塊控制side的大小

side.style.width = 400 * scale + "px";

side.style.height = 400 * scale + "px";

//通過滑塊控制side的顏色的透明度

side.style.opacity = scale;

side.style.filter = "alpha(opacity:" + scale * 100 + ")";

 

}

document.onmouseup = function() {

document.onmousemove = null;

document.onmouseup = null;

}

return false;

 

}

</script>

 

</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM