JS實(shí)現(xiàn)拖動模糊框特效
本文實(shí)例為大家分享了JS實(shí)現(xiàn)拖動模糊框特效的具體代碼,供大家參考,具體內(nèi)容如下
需求:
在圖片上拖動按鈕,圖片蒙層慢慢覆蓋,當(dāng)蒙層邊緣碰到左右下角文字時(shí),文字隱藏。
技術(shù):
監(jiān)聽器,鼠標(biāo)坐標(biāo)獲取
效果圖

源碼分享:
HTML
<h1>Image Comparison Slider</h1>
<nav>
<!--底層圖--> <img src="img/img-original.jpg" alt="">
<!--蒙版使用背景圖--> <div id="img">
<h3 id="leftBottom">Modified</h3>
<!--拖動按鈕--> <button id="btn">
<span class="iconfont icon-zuojiantou"></span>
<span class="iconfont icon-youjiantou"></span>
</button>
</div>
<h3 id="rightBottom">Original</h3>
</nav>
CSS樣式
<style>
* {
margin: 0;
padding: 0;
color: #E8F6F5;
}
body {
background-color: #566B89;
padding-top: 1px;
}
nav {
width: 1200px;
height: 675px;
overflow-x: hidden;
position: relative;
margin: 100px auto 0;
}
h1 {
text-align: center;
margin-top: 100px;
font-weight: 400;
}
nav>img {
position: absolute;
}
#img {
width: 600px; /*初始狀態(tài)顯示一半蒙層*/
height: 675px;
background: url("img/img-modified.jpg"); /*這里容器大小與圖片一致,若想改變大小,設(shè)置背景大小屬性 background-size: 圖片寬 圖片高;*/
position: relative;
animation: start 1s ease-in-out;
}
#img img {
width: 100%;
height: 100%;
}
@keyframes start {
0% {
width: 0;
}
50% {
width: 650px;
}
100% {
width: 600px;
}
}
#btn {
position: absolute;
right: -25px;
top: calc(50% - 25px);
width: 56px;
height: 56px;
line-height: 56px;
border: none;
outline: none;
border-radius: 50%;
background-color: pink;
font-size: 0;
text-align: center;
color: white;
cursor: pointer;
}
.iconfont {
font-size: 20px;
}
h3 {
font-weight: 400;
color: white;
}
#leftBottom,#rightBottom {
position: absolute;
width: 100px;
bottom: 50px;
}
#leftBottom {
left: 50px;
}
#rightBottom {
right: 50px;
}
</style>
JS部分
<script>
let img = document.querySelector("#img");
let btn = document.querySelector("#btn");
let nav = document.querySelector("nav");
let leftBottom = document.querySelector("#leftBottom");
let rightBottom = document.querySelector("#rightBottom");
btn.onmousedown = function (e) {
let clientx = e.clientX; // 點(diǎn)擊獲取鼠標(biāo)初始X坐標(biāo)
nav.onmousemove = function () {
let e = arguments[0] || window.event;
let X = e.clientX; // 移動時(shí)獲取鼠標(biāo)移動時(shí)的X坐標(biāo)
let imgW = parseInt(getComputedStyle(img,null).width);
img.style.width = `${ imgW + X - clientx}px`; // 圖片寬度 = 移動時(shí)的X坐標(biāo) - 點(diǎn)擊時(shí)的初始坐標(biāo) 也就是 圖片寬度 + 鼠標(biāo)X坐標(biāo)的偏移量
clientx = X ; // 將最新的位置的X坐標(biāo) 賦值給 點(diǎn)擊初始坐標(biāo) 也就是 更新 X坐標(biāo)初始值
if (imgW < 150){
leftBottom.style.display = "none";
}else {
leftBottom.style.display = "block";
}
if (imgW > 1050){
rightBottom.style.display = "none";
}else {
rightBottom.style.display = "block";
}
}
};
document.onmouseup = function () {
nav.onmousemove = null;
}
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS拖動鼠標(biāo)畫出方框?qū)崿F(xiàn)鼠標(biāo)選區(qū)的方法
- angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動效果
- JS+CSS實(shí)現(xiàn)可拖動的彈出提示框
- 原生js實(shí)現(xiàn)可拖動的登錄框效果
- js實(shí)現(xiàn)簡單模態(tài)框?qū)嵗?/a>
- AngularJs 彈出模態(tài)框(model)
- JS實(shí)現(xiàn)圖片點(diǎn)擊后出現(xiàn)模態(tài)框效果
- Vue.js彈出模態(tài)框組件開發(fā)的示例代碼
- 原生js實(shí)現(xiàn)簡單的模態(tài)框示例
- js實(shí)現(xiàn)類bootstrap模態(tài)框動畫
- JavaScript實(shí)現(xiàn)可拖動模態(tài)框
相關(guān)文章
JavaScript實(shí)現(xiàn)前端飛機(jī)大戰(zhàn)小游戲
詳解JavaScript基于面向?qū)ο笾^承實(shí)例
JS 實(shí)現(xiàn)Base64編碼與解碼實(shí)例詳解
利用js制作html table分頁示例(js實(shí)現(xiàn)分頁)
給before和after偽元素設(shè)置js效果的方法

