html 小圆点图标,HTML 滑块图标

这篇博客详细介绍了如何使用HTML、CSS和JavaScript实现一个滑块组件,包括滑块的样式设置、事件监听以及拖动操作。通过CSS定义了滑块的外观,JavaScript处理了滑块的点击和拖动事件,实时更新滑块位置并显示当前位置百分比。
摘要由CSDN通过智能技术生成

效果截图:css

0badb0ba9fbf631e735bf53f485cd8d8.png

Html页:html

滑块图标

当前位置0%

ide

css样式:3d

.SliderIcon {

width: 250px;

height: 80px;

margin: 100px auto;

position: relative;

}htm

.bar {

width: 250px;

height: 5px;

border-radius: 10px;

background: #aaa;

background-size: 30% 0%;

position: absolute;

top: 0;

bottom: 0;

left: 0;

right: 0;

margin: auto;

cursor: pointer;

}

.barContent {

height: 5px;

border-radius: 10px;

background: #1199c6;

position: absolute;

cursor: pointer;

}

.box {

width: 22px;

height: 22px;

background: #1199c6;

position: absolute;

bottom: 0;

top: 0;

margin: auto 0;

border-radius: 50%;

cursor: pointer;

transition: left 0.1s linear 0s;

text-align: center;

font-size: 14px;

color: #ffffff;

font-weight:bold;

}blog

JS逻辑:事件

var box = document.getElementsByClassName('box')[0]

var bar = document.getElementsByClassName('bar')[0]

var SliderIcon = document.getElementsByClassName('SliderIcon')[0]

var p = document.getElementsByTagName('p')[0]

var cha = bar.offsetWidth - box.offsetWidth

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

box.onmousedown = function (ev) {

let boxL = box.offsetLeft

let e = ev || window.event //兼容性

let mouseX = e.clientX //鼠标按下的位置

window.onmousemove = function (ev) {

let e = ev || window.event

let moveL = e.clientX - mouseX //鼠标移动的距离

let newL = boxL + moveL //left值

// 判断最大值和最小值

if (newL < 0) {

newL = 0

}

if (newL >= cha) {

newL = cha

}

// 计算比例

let bili = newL / cha * 100;

// 改变left值

box.style.left = barContent.style.width = newL + 'px';

p.innerHTML = '当前位置' + Math.ceil(bili) + '%'

return false //取消默认事件

}

window.onmouseup = function () {

window.onmousemove = false //解绑移动事件

return false

}

return false

};

// 点击音量条

bar.onclick = function (ev) {

let left = ev.clientX - SliderIcon.offsetLeft - box.offsetWidth / 2

if (left < 0) {

left = 0

}

if (left >= cha) {

left = cha

}

let bili = left / cha * 100;

box.style.left = barContent.style.width = left + 'px';

p.innerHTML = '当前位置' + Math.ceil(bili) + '%'

return false

}ip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值