原生js實現放大鏡效果


  今天做任務的時候,有一個任務就是讓實現電商網站常用的放大鏡效果,類似於這樣的放大鏡效果效果,之前並沒有做過這種放大鏡效果,剛開始的思路是對圖片進行裁剪,但是后來發現實在是難以實現,於是求助了萬能的谷歌,發現一個很好的思路就是,展示區是一小塊可視區域,給他一個圖片,超出可視區域的部分設為隱藏,有了這個思路,這個效果就能夠很好的實現了,先看一下HTML結構!

  

<div id="pic_wrap">
    <div id="float_box"></div>
    <img src="1.jpg">
</div>
<div id="show">
    <img src="1.jpg" id="big_img">
</div>

  最外層的兩個div,分別是商品原始圖片區域,和放大后的可視區域!id為float_box的區域為放大鏡所示的區域!第一個img為商品圖片,第二個img為預留的放大后的商品!圖片CSS代碼如下!

        img {
            width: 250px;
            height: 150px;
        }
        #pic_wrap {
            position: relative;
            width: 250px;
            height: 150px;
        }
        #float_box {
            width: 100px;
            height: 100px;
            background-color: green;
            filter: opacity(alpha: 30);
            opacity: 0.3;
            position: absolute;
            display: none;
        }
        #big_img {
            background-image: url(1.jpg);
            height: 450px;
            width: 750px;
            background-repeat: no-repeat;
            background-size: cover;
            position: relative;
        }
        #show {        
            width: 300px;
            height: 300px;
            background-color: white;
            opacity: 1;
            filter: opacity(alpha:1);
            overflow: hidden;
            display: none;
        }        

HTML和CSS寫好之后,就要利用js給放大鏡加一些交互效果!

  第一步,當鼠標移入的時候,放大鏡能夠顯示出來!需要加onmouseover事件。

  第二步,當鼠標沒有移除,且鼠標在圖片內不停地移動, 需要加onmousemove事件。

  第三步,當鼠標完全移除后,需要加onmouseout事件。

  第四步,onmouseover事件需要讓放大鏡和可視窗口顯示出來。

  第五步,onmousemove事件,讓放大鏡和可視窗口中的圖片同時移動。

  第六步,onmouseout時間,讓放大鏡和可是窗口消失!

完整代碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        /*body {
            margin: 0;
            padding: 0;
        }*/
        img {
            width: 250px;
            height: 150px;
        }
        #pic_wrap {
            position: relative;
            width: 250px;
            height: 150px;
        }
        #float_box {
            width: 100px;
            height: 100px;
            background-color: green;
            filter: opacity(alpha: 30);
            opacity: 0.3;
            position: absolute;
            display: none;
        }
        #big_img {
            background-image: url(1.jpg);
            height: 450px;
            width: 750px;
            background-repeat: no-repeat;
            background-size: cover;
            position: relative;
        }
        #show {        
            width: 300px;
            height: 300px;
            background-color: white;
            opacity: 1;
            filter: opacity(alpha:1);
            overflow: hidden;
            display: none;
        }
    </style>
</head>
<body>
    <div id="pic_wrap">
        <div id="float_box"></div>
        <img src="1.jpg">
    </div>
    <div id="show">
        <img src="1.jpg" id="big_img">
    </div>
<script src="等比放大鏡.js"></script>
<script type="text/javascript">    
    var pic_wrap = document.getElementById('pic_wrap'),
        float_box = document.getElementById("float_box"),
        show = document.getElementById('show');
        big_img = document.getElementById("big_img");
    //鼠標移入事件,讓放大鏡和放大區顯示!
    pic_wrap.onmouseover = function() {
        float_box.style.display = "block";
        show.style.display = "block";
    }
    //鼠標不斷移動時觸發,實時更新放大鏡得到的圖片
    pic_wrap.onmousemove = function(event) {
        floatMove(float_box, pic_wrap, event);
        showPic();
    }
    //鼠標移出后,放大鏡和放大區隱藏
    pic_wrap.onmouseout = function() {
        float_box.style.display = "none";
        show.style.display = "none";
    }    
            //由於offset方法包括邊框,在使用的時候很容易出現問題,所以用style來實時獲取attr!
    function getStyle(obj, attr) {
        if (window.currentStyle) {
            return parseInt(obj.currentStyle[attr]);
        }
        else {
            return parseInt(getComputedStyle(obj,null)[attr]);
        }
    }
    //運動框架,控制放大鏡在原圖中的位置!
    function floatMove(argu1, argu2, event) {
        var e = event ||window.event;
        var minLeft = getStyle(argu1, "width");
        var maxLeft = getStyle(argu2, "width") - minLeft/2;
        var minHeight = getStyle(argu1, "height");
        var maxHeight = getStyle(argu2, "height") - minHeight/2;
        console.log(maxLeft);
        console.log(maxLeft - minLeft/2);
        if (e.clientX < minLeft/2) {
            float_box.style.left = "0px";//防止放大鏡超出左邊框
        }
        else if (e.clientX > maxLeft) {
            float_box.style.left = getStyle(argu2, "width") - getStyle(argu1, "width") + "px";//防止放大鏡超出右邊框
        }
        else {
            float_box.style.left = event.clientX - minLeft/2 + "px"; //放大鏡完全在圖片內時正常移動
        }
        if (e.clientY < minHeight/2) {
            float_box.style.top = "0px"; //防止放大鏡超出上邊框
        }
        else if (e.clientY > maxHeight) {
            float_box.style.top = getStyle(argu2, "height") - getStyle(argu1, "height") + "px"; //防止放大鏡超出下邊框
        }
        else {
            float_box.style.top = event.clientY - minLeft/2 + "px";
        }
    }
    //用來顯示放大鏡得到的圖片,利用坐標計算!實時更新可視區域
    function showPic() {
        var iCurLeft = getStyle(float_box,"left");
        var iCurTop = getStyle(float_box,"top");
        var a = getStyle(pic_wrap,"width") - getStyle(float_box,"width");
        var b = getStyle(big_img,"width") - getStyle(show,"width");        
        var moveWidth = -(iCurLeft/a)*b;
        big_img.style.left = moveWidth + "px";
        var c = getStyle(pic_wrap,"height") - getStyle(float_box,"height");
        var d = getStyle(big_img,"height") - getStyle(show,"height");
        var moveHigth = -(iCurTop/c)*d;
        big_img.style.top = moveHigth + "px";
    }
</script>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM