Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
{{ percent }}
css
:root { --color-primary: #5626e8; --color-secondary: #d2daef; --color-bg: #f4f6fe; } * { box-sizing: border-box; } body { background-color: var(--color-bg); } main { width: 100%; min-height: 100vh; display: flex; justify-content: center; align-items: center; } .sliderContainer { position: relative; width: 100%; display: flex; flex-direction: column; align-items: center; padding: 3em; } .sliderContainer .sliderContainer__input { width: 100%; -webkit-appearance: none; appearance: none; outline: none; height: 0.2em; background-image: -webkit-gradient( linear, left top, right top, color-stop(50%, var(--color-primary)), color-stop(50%, var(--color-secondary)) ); border-radius: 1em; margin: 0; } .sliderContainer .sliderContainer__thumb { cursor: pointer; width: 1.5em; height: 1.5em; border: 0.5em solid var(--color-primary); border-radius: 100%; background-color: white; position: absolute; top: 50%; transform: translateY(-50%); user-select: none; } .ballon { --size: 4em; width: var(--size); height: var(--size); display: flex; justify-content: center; align-items: center; background-color: var(--color-primary); border-bottom-left-radius: 50%; position: absolute; border-top-left-radius: 50%; border-top-right-radius: 50%; top: -1.2em; opacity: 0; } .ballon .ballon__text { color: white; transform: rotate(-45deg); font-family: Arial, Helvetica, sans-serif; } .ballon::after { content: ""; position: absolute; width: 0; height: 0; border-left: 0.25em solid transparent; border-right: 0.25em solid transparent; transform: rotate(-45deg); border-bottom: 0.25em solid var(--color-primary); top: 98%; left: 94%; } @media (max-width: 768px) { .sliderContainer { min-width: 95%; } }
JavaScript
/** 让气球飞! 尝试将范围设置在 80 到 100 之间以查看飞行动画。 **/ let currentDeltaX = 0; //Utils const getElementOffset = el => el.getBoundingClientRect(); const getPercentInBetween = ({ value, min, max }) => { return (value - min) / (max - min) * 100; }; const getValueInBetween = (value, { min, max }) => { if (value < min) return min; if (value > max) return max; return value; }; const getSliderValue = () => { const getRef = ref => app.$refs[ref]; const { left: thumbElLeft, width: thumbElWidth } = getElementOffset( getRef("thumbEl") ); const { left: inputElLeft, right: inputElRight } = getElementOffset( getRef("inputEl") ); const thumbCenterPoint = thumbElLeft + thumbElWidth / 2; const rangeValue = getPercentInBetween({ value: thumbCenterPoint, min: inputElLeft, max: inputElRight }); const percent = Math.round( getValueInBetween(rangeValue, { min: 0, max: 100 }) ); return percent; }; const getBallonScaleValue = () => 1 + getValueInBetween(getSliderValue(), { min: 1, max: 99 }) / 480; //------------------- const app = new Vue({ el: "main", data: { isDragging: false, percent: 50, initialThumbElClientRect: null }, mounted: function() { const getRef = ref => this.$refs[ref]; TweenLite.set(getRef("thumbEl"), { y: "-50%" }); TweenLite.set(getRef("ballonEl"), { rotation: 45 }); this.initialThumbElClientRect = getElementOffset(getRef("thumbEl")); }, methods: { getRef: function(ref) { return this.$refs[ref]; }, dragStart: function() { this.isDragging = true; TweenLite.set(this.getRef("ballonEl"), { rotation: 45 }); this.percent = getSliderValue(); this.toggleSliderAnimation(); }, dragEnd: function() { this.isDragging = false; this.toggleSliderAnimation(); }, drag: function(event) { if (this.isDragging) { const { deltaX: newDeltaX, velocityX } = event; const { left: thumbElInitialOffsetLeft, right: thumbElInitialOffsetRight, width: thumbElWidth } = this.initialThumbElClientRect; const { left: inputElOffsetLeft, right: inputElOffsetRight } = getElementOffset(this.getRef("inputEl")); const xMovement = newDeltaX + currentDeltaX; const hasGoneTooLeft = thumbElInitialOffsetLeft + xMovement < inputElOffsetLeft - thumbElWidth / 2; const hasGoneTooRight = thumbElInitialOffsetRight + xMovement > inputElOffsetRight + thumbElWidth / 2; if (hasGoneTooLeft || hasGoneTooRight) return; this.moveThumb({ x: xMovement, velocityX }); } }, moveThumb: function({ x, velocityX }) { const sliderValue = getSliderValue(); const tl = new TimelineLite(); const scaleValue = getBallonScaleValue(); tl .to(this.getRef("thumbEl"), 0.001, { x, ease: Power4.ease }) .to(this.getRef("ballonEl"), 0.2, { x, scale: scaleValue, rotation: 45 + getValueInBetween(velocityX, { min: -7, max: 7 }) * -15, ease: Power4.ease }); this.getRef("inputEl").style.backgroundImage = `-webkit-gradient( linear, left top, right top, color-stop(${sliderValue}%, var(--color-primary)), color-stop(${sliderValue}%, var(--color-secondary)) )`; this.percent = sliderValue; }, toggleSliderAnimation: function() { const that = this; const tl = new TimelineLite(); const sliderValue = getSliderValue(); const scaleValue = this.isDragging ? getBallonScaleValue() : 0.8; const shouldFlyBalloon = sliderValue >= 80 && sliderValue <= 100; const toggleThumbAnim = () => { tl.to(that.getRef("thumbEl"), 0.23, { width: that.isDragging ? "2.6em" : "1.5em", height: that.isDragging ? "2.6em" : "1.5em", borderWidth: that.isDragging ? "2px" : "0.5em", ease: Power4.easeOut }); }; const toggleBallonAnim = () => { tl.to( that.getRef("ballonEl"), 0.5, { opacity: that.isDragging ? 1 : 0, scale: scaleValue, y: that.isDragging ? "-75%" : "5%", ease: that.isDragging ? Power1.easeOut : Power1.easeIn }, "-0.23" ); }; if (this.isDragging) { toggleThumbAnim(); toggleBallonAnim(); } else { toggleThumbAnim(); if (shouldFlyBalloon) { tl.to(this.getRef("ballonEl"), 1.7, { y: -window.innerHeight / 2, opacity: 0, ease: Power1.easeOut, onComplete: () => { TweenLite.set(this.getRef("ballonEl"), { y: "5%" }); } }); } else { toggleBallonAnim(); } } } } }); const getRef = ref => app.$refs[ref]; const appHammer = new Hammer.Manager(getRef("thumbEl")); appHammer.add(new Hammer.Pan({ threshold: 0 })); appHammer.on("pan", app.drag); appHammer.on("panend", ({ deltaX }) => { currentDeltaX += deltaX; }); window.addEventListener("mouseup", app.dragEnd, false); window.addEventListener("touchend", app.dragEnd, false);
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>气球滑块-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号