最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實現(xiàn)自定義滾動條的示例

 更新時間:2020年10月27日 08:35:51   作者:星輝  
這篇文章主要介紹了js實現(xiàn)自定義滾動條的示例,幫助大家制作JS特效,美化自身網(wǎng)頁,感興趣的朋友可以了解下

自定義滾動條

目錄

  • 代碼實例
  • 代碼解析
  • 下載源碼鏈接

代碼實例

* {
	padding: 0;
	margin: 0;
}
#box1 {
	width: 500px;
	height: 20px;
	background: #999;
	position: relative;
	margin: 20px auto;
}
#box2 {
	width: 20px;
	height: 20px;
	background: green;
	position: absolute;
}
#box3 {
	width: 0;
	height: 0;
	margin: 20px auto;
}
#box3 img {
	width: 100%;
	height: 100%;
}

<div id="box1">
<div id="box2"></div>
</div>
<div id="box3">
	<img src="./1.jpg">
</div>

// 獲取dom元素
var oBox1 = document.getElementById('box1');
var oBox2 = document.getElementById('box2');
var oBox3 = document.getElementById('box3');

// 按下滾動條后的操作
oBox2.onmousedown = function(e) {
// 獲取事件的必備操作,保證事件被獲取
var oEvent = e || event

// 保證只有被按下滾動條后才能執(zhí)行此函數(shù)
document.onmousemove = function(e) {
	var oEvent = e || event
	var l = oEvent.clientX - oBox1.offsetLeft
	// 獲取滾動條可活動的寬度范圍
	var wid = oBox1.offsetWidth - oBox2.offsetWidth
	if (l < 0) {
		l = 0
	} else if (l > wid) {
		l = wid
	}
	// 位置定位
	oBox2.style.left = l + 'px'

	// 根據(jù)滾動條位置獲得比例
	var scale = l / wid
	// 圖片的寬度和高度
	var w = 3264 * scale
	var h = 4080 * scale
	// oBox3.style.cssText是加在內(nèi)嵌style中的
	oBox3.style.cssText += 'width:' + w + 'px;height:' + h + 'px;'
}

// 保證鼠標松開后事件不再執(zhí)行
document.onmouseup = function() {
	document.onmousemove = null
	document.onmousedown = null
}
}

代碼解析

elem.style.cssText是加在內(nèi)嵌style中的

// oBox3.style.cssText是加在內(nèi)嵌style中的
oBox3.style.cssText += 'width:' + w + 'px;height:' + h + 'px;

下載源碼鏈接

星輝的Github

以上就是js實現(xiàn)自定義滾動條的示例的詳細內(nèi)容,更多關(guān)于js實現(xiàn)自定義滾動條的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

武威市| 商丘市| 甘泉县| 晴隆县| 万源市| 阳原县| 仙居县| 来凤县| 策勒县| 鄂托克前旗| 隆子县| 高清| 新巴尔虎左旗| 教育| 特克斯县| 佛冈县| 兴文县| 德格县| 遵义市| 邵阳县| 石首市| 邓州市| 虎林市| 长白| 札达县| 鹤山市| 沾化县| 平阳县| 交口县| 定日县| 惠来县| 克东县| 玛曲县| 都江堰市| 即墨市| 杭锦旗| 江门市| 偏关县| 建始县| 务川| 蒙阴县|