JS實現(xiàn)拖動模糊框特效
更新時間:2020年08月25日 08:53:22 作者:hthththtt
這篇文章主要為大家詳細介紹了JS實現(xiàn)拖動模糊框特效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JS實現(xiàn)拖動模糊框特效的具體代碼,供大家參考,具體內(nèi)容如下
需求:
在圖片上拖動按鈕,圖片蒙層慢慢覆蓋,當蒙層邊緣碰到左右下角文字時,文字隱藏。
技術:
監(jiān)聽器,鼠標坐標獲取
效果圖
源碼分享:
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"); /*這里容器大小與圖片一致,若想改變大小,設置背景大小屬性 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; // 點擊獲取鼠標初始X坐標 nav.onmousemove = function () { let e = arguments[0] || window.event; let X = e.clientX; // 移動時獲取鼠標移動時的X坐標 let imgW = parseInt(getComputedStyle(img,null).width); img.style.width = `${ imgW + X - clientx}px`; // 圖片寬度 = 移動時的X坐標 - 點擊時的初始坐標 也就是 圖片寬度 + 鼠標X坐標的偏移量 clientx = X ; // 將最新的位置的X坐標 賦值給 點擊初始坐標 也就是 更新 X坐標初始值 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- JS拖動鼠標畫出方框?qū)崿F(xiàn)鼠標選區(qū)的方法
- angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動效果
- JS+CSS實現(xiàn)可拖動的彈出提示框
- 原生js實現(xiàn)可拖動的登錄框效果
- js實現(xiàn)簡單模態(tài)框?qū)嵗?/a>
- AngularJs 彈出模態(tài)框(model)
- JS實現(xiàn)圖片點擊后出現(xiàn)模態(tài)框效果
- Vue.js彈出模態(tài)框組件開發(fā)的示例代碼
- 原生js實現(xiàn)簡單的模態(tài)框示例
- js實現(xiàn)類bootstrap模態(tài)框動畫
- JavaScript實現(xiàn)可拖動模態(tài)框
相關文章
JavaScript輸入分鐘、秒倒計時技巧總結(jié)(附代碼)
這篇文章主要介紹了JavaScript輸入分鐘、秒倒計時的代碼實現(xiàn),通過css和js代碼展示了邏輯過程,具體操作步驟大家可查看下文的詳細講解,感興趣的小伙伴們可以參考一下。2017-08-08微信小程序onShareTimeline()實現(xiàn)分享朋友圈
這篇文章主要給大家介紹了關于微信小程序onShareTimeline()實現(xiàn)分享朋友圈的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01詳談js使用in和hasOwnProperty獲取對象屬性的區(qū)別
下面小編就為大家?guī)硪黄斦刯s使用in和hasOwnProperty獲取對象屬性的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04詳解JavaScript中Generator函數(shù)的使用
Generator 是 ES6 新增的一種函數(shù)類型,這篇文章主要來和大家詳細聊聊Generator函數(shù)的具體用法,文中的示例代碼講解詳細,感興趣的可以了解一下2023-06-06