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

js實(shí)現(xiàn)自定義滾動(dòng)條的示例

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

自定義滾動(dòng)條

目錄

  • 代碼實(shí)例
  • 代碼解析
  • 下載源碼鏈接

代碼實(shí)例

* {
	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');

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

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

	// 根據(jù)滾動(dòng)條位置獲得比例
	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;'
}

// 保證鼠標(biāo)松開后事件不再執(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實(shí)現(xiàn)自定義滾動(dòng)條的示例的詳細(xì)內(nèi)容,更多關(guān)于js實(shí)現(xiàn)自定義滾動(dòng)條的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

青阳县| 芦溪县| 福泉市| 怀集县| 平定县| 泰兴市| 高陵县| 奇台县| 长武县| 商洛市| 龙胜| 台湾省| 芒康县| 秭归县| 嘉禾县| 积石山| 濮阳市| 宁阳县| 昌吉市| 尼勒克县| 威远县| 赤城县| 桐城市| 定结县| 尼玛县| 右玉县| 新龙县| 苏尼特右旗| 东丽区| 吴堡县| 蒙阴县| 保靖县| 通河县| 新民市| 巴林右旗| 蒙阴县| 盘锦市| 长泰县| 来凤县| 孝昌县| 尚志市|