Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
* { -webkit-tap-highlight-color: transparent; box-sizing: border-box; } :root { --hue: 216; --light: 37.5; } body { background: hsl(var(--hue), 50%, calc(var(--light) * 1%)); cursor: var(--cursor); min-height: 100vh; touch-action: none; } .switch { --size: 200; --panel-hue: 55; --panel-saturation: 10; --faceplate: hsl(55, 10%, calc((30 + var(--light)) * 1%)); --faceplate-light: hsl(55, 10%, calc((50 + var(--light)) * 1%)); --faceplate-dark: hsl(55, 10%, calc((0 + var(--light)) * 1%)); --pressed: 0; background: linear-gradient(0deg, var(--faceplate-dark), var(--faceplate-light)); border-radius: 5%; height: calc(var(--size) * 1px); overflow: hidden; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); width: calc(var(--size) * 1px); box-shadow: 0px 10px 0px 0px hsl(var(--hue), 50%, calc((var(--light) - 10) * 1%)); } .switch__faceplate { background: var(--faceplate); height: calc(100% - 10px); width: calc(100% - 10px); position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); border-radius: 5%; } .switch__faceplate:after, .switch__faceplate:before { --screw: hsl(0, 0%, calc((20 + var(--light)) * 1%)); --screw-dark: hsl(0, 0%, calc((0 + var(--light)) * 1%)); --screw-darkest: hsl(0, 0%, calc((-20 + var(--light)) * 1%)); content: ''; height: 8%; width: 8%; background: linear-gradient(calc(var(--rotation, 30) * 1deg), var(--screw) 0%, var(--screw) 45%, var(--screw-dark) 45%, var(--screw-dark) 55%, var(--screw) 55%); box-shadow: 0px 1px 0px 1px var(--screw-darkest) inset; border-radius: 50%; top: 50%; -webkit-transform: translate(0, -50%); transform: translate(0, -50%); position: absolute; } .switch__faceplate:after { --rotation: 120; right: 10%; } .switch__faceplate:before { left: 10%; } .switch__button { height: 35%; width: 35%; border: 0; background: var(--faceplate); border-radius: 50%; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); outline: transparent; padding: 0; cursor: pointer; } .switch__button:before { content: ''; height: 99%; width: 99%; background: var(--faceplate-dark); position: absolute; border-radius: 50%; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%) translate(0, calc((10 - (var(--pressed) * 10)) * 1px)); transform: translate(-50%, -50%) translate(0, calc((10 - (var(--pressed) * 10)) * 1px)); transition: -webkit-transform 0.15s ease 0s; transition: transform 0.15s ease 0s; transition: transform 0.15s ease 0s, -webkit-transform 0.15s ease 0s; z-index: -1; } .switch__button-face { border: 4px solid var(--faceplate-light); height: 100%; width: 100%; background: var(--faceplate); border-radius: 100%; -webkit-transform: rotate(calc(var(--rotation, 0) * 1deg)); transform: rotate(calc(var(--rotation, 0) * 1deg)); } .switch__button-dim-marker { position: absolute; left: 50%; height: 44px; width: 44px; border-radius: 50%; top: 10%; -webkit-transform: translate(-50%, -90%); transform: translate(-50%, -90%); cursor: var(--cursor, 'grab'); } .switch__button-dim-marker:after { content: ''; background: hsl(0, 100%, calc((0 + var(--light)) * 1%)); height: 10px; width: 10px; border-radius: 100%; position: absolute; top: 100%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); }
JavaScript
/ ** *两点之间的角度 * @param {Object}事件-当前坐标的指针事件 * @param {Object}元素-用作中心点的元素 * @param {Number}缓冲区-缓冲区,以便角度不允许手柄和轨道重叠 * / const getAngle = (event, element) => { const { clientX: x, clientY: y } = event.touches && event.touches.length ? event.touches[0] : event; const { x: handleX, y: handleY, width: handleWidth, height: handleHeight } = element.getBoundingClientRect(); const handleCenterPoint = { x: handleX + handleWidth / 2, y: handleY + handleHeight / 2 }; const angle = Math.atan2(handleCenterPoint.y - y, handleCenterPoint.x - x) * 180 / Math.PI; return angle; }; const button = document.querySelector('.switch__button'); const dimmer = document.querySelector('.switch__button-dim-marker'); let DOWN_EVENT = 'mousedown'; let MOVE_EVENT = 'mousemove'; let UP_EVENT = 'mouseup'; if (window.PointerEvent) { // PointerEvent, Chrome DOWN_EVENT = 'pointerdown'; MOVE_EVENT = 'pointermove'; UP_EVENT = 'pointerup'; } else if ('ontouchstart' in window) { // Touch Events, iOS Safari DOWN_EVENT = 'touchstart'; MOVE_EVENT = 'touchmove'; UP_EVENT = 'touchend'; } let ON = true; const BUFFER = 45; const VARIANTS = { ON: 60, OFF: 10, LOWER: 15, UPPER: 60, DIMMED: 0.5 }; const setLight = () => { document.documentElement.style.setProperty( '--light', ON ? VARIANTS.LOWER + (VARIANTS.UPPER - VARIANTS.LOWER) * VARIANTS.DIMMED : VARIANTS.OFF); }; const dim = e => { let rotation; const angle = getAngle(e, button); if (angle > 0) { rotation = angle - 90; } else { rotation = 270 + angle; if (angle > -(90 + BUFFER) && angle < -(90 - BUFFER) && angle < -90) { rotation = 270 - (90 + BUFFER); VARIANTS.DIMMED = 1; } if (angle > -(90 + BUFFER) && angle < -(90 - BUFFER) && angle > -90) { rotation = 270 - (90 - BUFFER); VARIANTS.DIMMED = 0; } } // console.info(rotation) // Lets work out the dimming here // If angle is rotation is greater than 0 and less than 180 - BUFFER // Dim is 0.5 + (angle / 135deg) * 0.5 if (rotation > 0 && rotation < 180 - BUFFER) { VARIANTS.DIMMED = 0.5 + rotation / (180 - BUFFER) * 0.5; } else if (rotation < 0) { // There's 180 - BUFFER degrees to play with but we know we have 90 degrees here. VARIANTS.DIMMED = 0.5 - Math.abs(rotation / (180 - BUFFER)) * 0.5; } else if (rotation > 180 + BUFFER && rotation < 270) { VARIANTS.DIMMED = (rotation - (180 + BUFFER)) / (180 - (90 + BUFFER)) * ( BUFFER / (180 - BUFFER) * 0.5); } button.style.setProperty('--rotation', rotation); setLight(); }; const endDim = () => { // dim(e) document.documentElement.style.setProperty('--cursor', 'initial'); dimmer.style.setProperty('--cursor', 'grab'); document.body.removeEventListener(MOVE_EVENT, dim); document.body.removeEventListener(UP_EVENT, endDim); }; const execute = () => { ON = !ON; button.style.setProperty('--pressed', 0); setLight(); document.body.removeEventListener(UP_EVENT, execute); }; const toggleLight = () => { button.style.setProperty('--pressed', 1); document.body.addEventListener(UP_EVENT, execute); }; const initDim = e => { e.stopPropagation(); document.documentElement.style.setProperty('--cursor', 'grabbing'); dimmer.style.setProperty('--cursor', 'grabbing'); document.body.addEventListener(MOVE_EVENT, dim); document.body.addEventListener(UP_EVENT, endDim); }; dimmer.addEventListener(DOWN_EVENT, initDim); button.addEventListener(DOWN_EVENT, toggleLight); setLight();
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>HSL调光旋钮开关-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号