最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javaScript實現(xiàn)放大鏡特效

 更新時間:2021年11月09日 08:36:24   作者:雪旭  
這篇文章主要為大家詳細介紹了javaScript實現(xiàn)放大鏡特效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

要實現(xiàn)的效果:鼠標(biāo)放到小圖片上小圖片上方會出現(xiàn)一個小塊,這個小塊里面的區(qū)域會放大顯示到右邊大圖里面(如下圖所示)

這個效果主要用到的是:鼠標(biāo)的坐標(biāo)e.clientX,e.clientY,偏移量offsetLeft,offsetTop,offsetWidth,sffsetHeight等屬性。

HTML和CSS代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            background:#2c3e50;

        }
        .wrap{
            display: flex;            
            position: relative;
            left: 200px;
            top: 30px;
        }
        .small{
            width: 500px;
            height: 300px;
            border-radius: 20px;
            overflow: hidden;
            position: relative;
            left: 0px;
        }
        .small img{
            width: 100%;
            height: 100%;
        }
        .small span{
            display: none;
            position: absolute;  
            left: 0;
            top: 0;      
            width: 100px;
            height: 100px;
            background: rgba(0,0,0,0.5);
            cursor: pointer;
            z-index: 1;
        }
        .big{
            display: none;
            width: 400px;
            height: 400px;
            overflow: hidden;
            position: relative;
            left: 50px;
            top: 0;
        }
        .big img{
            position: absolute;
            left: 0;
            top: 0; 
            width: 1000px;
            height: 600px;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="small">
            <img src="img/33.jpg" alt="">
            <span></span>
        </div>
        <div class="big">
            <img src="img/33.jpg" alt="">
        </div>
    </div>

</body>
</html>

JS部分:

鼠標(biāo)移入放大鏡(小圖上的小塊)顯示,右邊大圖顯示

//最大的容器
let wrap=document.querySelector('.wrap');
//小圖部分
let smallWrap=document.querySelector('.wrap .small');
let smallImg=document.querySelector('.wrap .small img');
let smallBox=document.querySelector('.wrap .small span');
//大圖部分
let bigBox=document.querySelector('.wrap .big');
let bigImg=document.querySelector('.wrap .big img');
smallWrap.onmouseover=function(){
       smallBox.style.display="block";
       bigBox.style.display="block";
}

鼠標(biāo)在小圖上移動時放大鏡跟隨移動,用event對象的event.clientX,和event.clientY來獲取鼠標(biāo)的坐標(biāo)。

通過event.clientX和event.clientY可以得到鼠標(biāo)的位置,父容器的偏移量,發(fā)大鏡的偏移量(實際項目中可能會給發(fā)大鏡設(shè)置偏移量,為了去掉這個偏移量的影響要減去這個偏移量),放大鏡效果中鼠標(biāo)一直都在小塊的中間位置,所以移動的位置就可以了這樣去的。

smallWrap.onmousemove=function(e){
            let moveX=e.clientX-wrap.offsetLeft-smallWrap.offsetLeft-smallBox.offsetWidth/2;
            let moveY=e.clientY-wrap.offsetTop-smallWrap.offsetTop-smallBox.offsetHeight/2;

            let maxX=smallWrap.offsetWidth-smallBox.offsetWidth;
            let maxY=smallWrap.offsetHeight-smallBox.offsetHeight;


            smallBox.style.left=moveX+'px'
            smallBox.style.top=moveY+'px'

       }

到這一步放大鏡就會跟隨鼠標(biāo)移動了,還要加個范圍限定,不然發(fā)大鏡移動距離就會超過小圖片了

范圍限定

smallWrap.onmousemove=function(e){
            let moveX=e.clientX-wrap.offsetLeft-smallWrap.offsetLeft-smallBox.offsetWidth/2;
            let moveY=e.clientY-wrap.offsetTop-smallWrap.offsetTop-smallBox.offsetHeight/2;

            let maxX=smallWrap.offsetWidth-smallBox.offsetWidth;
            let maxY=smallWrap.offsetHeight-smallBox.offsetHeight;

           
            //范圍限定方法一
           /*  if(moveX<0){
                moveX=0;
            }else if(moveX>=maxX){
                moveX=maxX
            }
            if(moveY<0){
                moveY=0;
            }else if(moveY>=maxY){
                moveY=maxY
            } */
             //范圍限定方法二
            moveX=Math.min(maxX,Math.max(0,moveX))
            moveY=Math.min(maxY,Math.max(0,moveY))

            smallBox.style.left=moveX+'px'
            smallBox.style.top=moveY+'px'

       }

放大鏡跟隨鼠標(biāo)移動實現(xiàn)之后,接下來就需要實現(xiàn)發(fā)大鏡移動時,大圖也跟著移動(大圖移動的方向是相反的),發(fā)大鏡移動的距離與大圖移動的距離是成正比的,小圖的寬度與大圖片(包過未顯示部分)的寬度也是成正比的,小圖可以移動動的最大距離和大圖可以動的最大距離也是成正比的,所以可以通過這二個公式求得大圖應(yīng)該移動多少。

放大鏡移動的距離/小圖的寬度=大圖移動的距離/大圖的寬度  這個公式雖然可以實現(xiàn)但是沒有限定最大可移動距離會出現(xiàn)這種效果

所以這個公式里要這樣寫最好放大鏡移動的距離/小圖的寬度-放大鏡的寬度(這是放大鏡最大的移動范圍)

放大鏡移動的距離/(小圖的寬度-放大鏡的寬度)=大圖移動的距離/(大圖的寬度-大圖顯示區(qū)域)

注意大圖移動的方向與放大鏡移動的方向是相反的!

smallWrap.onmousemove=function(e){
            let moveX=e.clientX-wrap.offsetLeft-smallWrap.offsetLeft-smallBox.offsetWidth/2;
            let moveY=e.clientY-wrap.offsetTop-smallWrap.offsetTop-smallBox.offsetHeight/2;

            let maxX=smallWrap.offsetWidth-smallBox.offsetWidth;
            let maxY=smallWrap.offsetHeight-smallBox.offsetHeight;

           
            //范圍限定方法一
           /*  if(moveX<0){
                moveX=0;
            }else if(moveX>=maxX){
                moveX=maxX
            }
            if(moveY<0){
                moveY=0;
            }else if(moveY>=maxY){
                moveY=maxY
            } */
             //范圍限定方法二
            moveX=Math.min(maxX,Math.max(0,moveX))
            moveY=Math.min(maxY,Math.max(0,moveY))

            smallBox.style.left=moveX+'px'
            smallBox.style.top=moveY+'px'

            let left=moveX/(smallImg.offsetWidth-smallBox.offsetWidth);//smallImg.offsetWidth-smallBox.offsetWidth最大移動位置
            let top=moveY/(smallImg.offsetHeight-smallBox.offsetHeight);

            bigImg.style.left=-left*(bigImg.offsetWidth-bigBox.offsetWidth)+'px'
            bigImg.style.top=-top*(bigImg.offsetHeight-bigBox.offsetHeight)+'px'
       }

最后再加上鼠標(biāo)移出事件,鼠標(biāo)移出,放大鏡和大圖隱藏

smallWrap.onmouseout=function(){
            smallBox.style.display="none";
            bigBox.style.display="none";
       }

全部代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            background:#2c3e50;

        }
        .wrap{
            display: flex;            
            position: relative;
            left: 200px;
            top: 30px;
        }
        .small{
            width: 500px;
            height: 300px;
            border-radius: 20px;
            overflow: hidden;
            position: relative;
            left: 100px;
        }
        .small img{
            width: 100%;
            height: 100%;
        }
        .small span{
            display: none;
            position: absolute;  
            left: 0;
            top: 0;      
            width: 100px;
            height: 100px;
            background: rgba(0,0,0,0.5);
            cursor: pointer;
            z-index: 1;
        }
        .big{
            display: none;
            width: 400px;
            height: 400px;
            overflow: hidden;
            position: relative;
            left: 120px;
            top: 0;
        }
        .big img{
            position: absolute;
            left: 0;
            top: 0; 
            width: 1000px;
            height: 600px;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="small">
            <img src="img/33.jpg" alt="">
            <span></span>
        </div>
        <div class="big">
            <img src="img/33.jpg" alt="">
        </div>
    </div>
    <script>
        //最大的容器
       let wrap=document.querySelector('.wrap');
       //小圖部分
       let smallWrap=document.querySelector('.wrap .small');
       let smallImg=document.querySelector('.wrap .small img');
       let smallBox=document.querySelector('.wrap .small span');
        //大圖部分
       let bigBox=document.querySelector('.wrap .big');
       let bigImg=document.querySelector('.wrap .big img');
       smallWrap.onmouseover=function(){
            smallBox.style.display="block";
            bigBox.style.display="block";
       }
       smallWrap.onmousemove=function(e){
            let moveX=e.clientX-wrap.offsetLeft-smallWrap.offsetLeft-smallBox.offsetWidth/2;
            let moveY=e.clientY-wrap.offsetTop-smallWrap.offsetTop-smallBox.offsetHeight/2;

            let maxX=smallWrap.offsetWidth-smallBox.offsetWidth;
            let maxY=smallWrap.offsetHeight-smallBox.offsetHeight;

           
            //范圍限定方法一
           /*  if(moveX<0){
                moveX=0;
            }else if(moveX>=maxX){
                moveX=maxX
            }
            if(moveY<0){
                moveY=0;
            }else if(moveY>=maxY){
                moveY=maxY
            } */
             //范圍限定方法二
            moveX=Math.min(maxX,Math.max(0,moveX))
            moveY=Math.min(maxY,Math.max(0,moveY))

            smallBox.style.left=moveX+'px'
            smallBox.style.top=moveY+'px'

            let left=moveX/(smallImg.offsetWidth-smallBox.offsetWidth);//smallImg.offsetWidth-smallBox.offsetWidth最大移動位置
            let top=moveY/(smallImg.offsetHeight-smallBox.offsetHeight);

            bigImg.style.left=-left*(bigImg.offsetWidth-bigBox.offsetWidth)+'px'
            bigImg.style.top=-top*(bigImg.offsetHeight-bigBox.offsetHeight)+'px'
       }
       smallWrap.onmouseout=function(){
            smallBox.style.display="none";
            bigBox.style.display="none";
       }
    </script>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何在JavaScript中比較日期詳解

    如何在JavaScript中比較日期詳解

    我們在日常開發(fā)過程中經(jīng)常會用到JavaScript語言在前端代碼中,進行日期的選擇,下面這篇文章主要給大家介紹了關(guān)于如何在JavaScript中比較日期的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • javascript模版引擎-tmpl的bug修復(fù)與性能優(yōu)化分析

    javascript模版引擎-tmpl的bug修復(fù)與性能優(yōu)化分析

    在平時編碼中,經(jīng)常要做拼接字符串的工作,如把json數(shù)據(jù)用HTML展示出來,以往字符串拼接與邏輯混在在一起會讓代碼晦澀不堪,加大了多人協(xié)作與維護的成本。而采用前端模板機制就能很好的解決這個問題
    2011-10-10
  • JavaScript 判斷一個對象{}是否為空對象的簡單方法

    JavaScript 判斷一個對象{}是否為空對象的簡單方法

    下面小編就為大家?guī)硪黄狫avaScript 判斷一個對象{}是否為空對象的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • 基于js實現(xiàn)checkbox批量選中操作

    基于js實現(xiàn)checkbox批量選中操作

    這篇文章主要為大家詳細介紹了基于js實現(xiàn)checkbox批量選中操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 不用一句js代碼初始化組件

    不用一句js代碼初始化組件

    不用一句js代碼初始化組件,是不是很神奇?
    2016-01-01
  • Javascript實現(xiàn)DIV滾動自動滾動到底部的代碼

    Javascript實現(xiàn)DIV滾動自動滾動到底部的代碼

    一個比較特殊的客戶要求,在一個頁面用表格顯示數(shù)據(jù),數(shù)據(jù)量不是很多,不希望使用瀏覽器的滾動條,只能在Div中滾動table中的數(shù)據(jù),但是有個特殊的要求,就是必須將滾動條自動滾動到底部
    2012-03-03
  • js創(chuàng)建對象的方式總結(jié)

    js創(chuàng)建對象的方式總結(jié)

    這篇文章主要介紹了js創(chuàng)建對象的方式,實例總結(jié)了3種常用的創(chuàng)建對象的方式,非常實用,需要的朋友可以參考下
    2015-01-01
  • javascript 使用sleep函數(shù)的常見方法詳解

    javascript 使用sleep函數(shù)的常見方法詳解

    這篇文章主要介紹了javascript 使用sleep函數(shù)的常見方法,結(jié)合實例形式分析總結(jié)了javascript sleep函數(shù)的功能、常見使用方法與操作注意事項,需要的朋友可以參考下
    2020-04-04
  • 原生JS實現(xiàn)的輪播圖功能詳解

    原生JS實現(xiàn)的輪播圖功能詳解

    這篇文章主要介紹了原生JS實現(xiàn)的輪播圖功能,結(jié)合實例形式分析了javascript實現(xiàn)輪播圖的原理、操作技巧與相關(guān)注意事項,需要的朋友可以參考下
    2018-08-08
  • javascript實現(xiàn)計算器功能

    javascript實現(xiàn)計算器功能

    這篇文章主要為大家詳細介紹了javascript實現(xiàn)計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評論

丹寨县| 睢宁县| 汝城县| 永仁县| 苗栗县| 德安县| 旬阳县| 新宁县| 全椒县| 沧源| 吉木萨尔县| 长海县| 涿州市| 贺州市| 余姚市| 札达县| 丰台区| 京山县| 宽甸| 开封市| 中宁县| 曲阜市| 呼和浩特市| 甘孜县| 五莲县| 札达县| 岐山县| 大同县| 祁东县| 工布江达县| 泊头市| 诸城市| 玉环县| 大冶市| 屯昌县| 麟游县| 崇左市| 大方县| 大荔县| 邵阳县| 巢湖市|