JavaScript鼠標懸停放大demo2

111.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    .main {
        position: relative;
        width: 990px;
        height: 1611px;
        margin: 0 auto;
        background-color: #EEE;
        margin-top: 20px;
    }

    .showImg {
        height: 370px;
        width: 310px;
        background-color: yellow;
        float: left;
        position: relative;
    }

    .showImg .show1 {
        width: 310px;
        height: 310px;
        background-color: purple;
        line-height: 310px;
        font-size: 0px;
        text-align: center;
        border: 1px solid silver;
    }

    .showImg .show1 #midImg {
        vertical-align: middle;
    }

    .showImg .show2 {
        margin-top: 10px;
        width: 270px;
        height: 40px;
        background-color: white;
        margin-left: 30px;

    }

    .showImg .show2 .smallImg li {
        float: left;
        text-align: center;
        margin-right: 10px;
        border: 1px solid silver;
        /* background-color: blue; */
    }



    .showImg .show2 .smallImg div {
        width: 40px;
        height: 40px;
        line-height: 40px;
        font-size: 0px;
        cursor: pointer;
    }

    .showImg .show2 .smallImg div img {
        vertical-align: middle;
    }

    #enlarge {
        position: absolute;
        width: 310px;
        height: 310px;
        /* background-color: #fff; */
        top: 0px;
        left: 320px;
        border: 1px solid silver;
        display: none;
        background: url('./img/01.jpg');
        background-repeat: no-repeat;
    }

    .showContent {
        box-sizing: border-box;
        height: 370px;
        width: 640px;
        background-color: hotpink;
        float: left;
        margin-left: 20px;
    }

    .showContent h4 {
        margin-top: 20px;
        margin-left: 28px;
        margin-bottom: 20px;
        display: inline-block;
        /* background-color: #fff; */
    }

    .showContent .con1 {
        margin: 0 auto;
        height: 150px;
        width: 600px;
        /* background-color: yellow; */
        /* html中超出盒子寬度會自動換行的,不用設置,如果你連續(xù)的英文之間沒用空格,
        那么它會以為是一個單詞,就不會換行,這個時候加上word-break: break-all; word-wrap:break-word; */
        /* word-break: break-all; word-wrap:break-word; */
    }

    .originalImg {
        position: absolute;
        top: 0px;
        left: 100px;
        display: none;

    }

    #square {
        cursor: move;
        /* width: 130px;
        height: 130px; */
        background: url('./img/mask2.png');
        /* opacity: 0.5; */
        left: 0px;
        top: 0px;
        position: absolute;

        /* display: none; */
    }

</style>

<body>
    <div class="main" id="main">
        <div class="showImg">

            <div class="show1" id="show1">
                <!-- 顯示圖片 -->
                <img id="midImg" src="./img/01_mid.jpg" alt="">

                <!-- 小正方塊 -->
                <div id="square">
                </div>
            </div>



            <!-- 下方小圖 -->
            <div class="show2">
                <ul class="smallImg" id="smallImg">
                    <li>
                        <div>
                            <a href="#">
                                <img src="./img/01_small.jpg" alt="">
                            </a>
                        </div>
                    </li>
                    <li>
                        <div>
                            <a href="#">
                                <img src="./img/02_small.jpg" alt="">
                            </a>
                        </div>
                    </li>
                    <li>
                        <div>
                            <a href="#">
                                <img src="./img/03_small.jpg" alt="">
                            </a>
                        </div>
                    </li>
                    <li>
                        <div>
                            <a href="#">
                                <img src="./img/04_small.jpg" alt="">
                            </a>
                        </div>
                    </li>
                    <li>
                        <div>
                            <a href="#">
                                <img src="./img/05_small.jpg" alt="">
                            </a>
                        </div>
                    </li>
                </ul>
            </div>

            <div id='enlarge'></div>

        </div>

        <div class="showContent">
            <h4>漫趣動漫游戲周邊七宗罪sin七大罪二次元短袖衣服原罪T恤夏季男女 </h4>
            <div class="con1">
                在上面的例子中,我們只在被選元素的開頭/結尾插入文本/HTML。 不過,append() 和 prepend() 方法能夠通過參數(shù)接收無限數(shù)量的新元素??梢酝ㄟ^ jQuery 來生成文本/HTML(就像上面的例子那樣),或者通過 JavaScript
                代碼和 DOM 元素。 在下面的例子中,我們創(chuàng)建若干個新元素。這些元素可以通過 text/HTML、jQuery 或者 JavaScript/DOM 來創(chuàng)建。然后我們通過 append() 方法把這些新元素追加到文本中(對
                prepend() 同樣有效):
            </div>
        </div>

        <div class="originalImg">
            <img id="bigImg" src="./img/01.jpg" />
        </div>
    </div>

    <script>
        var show1 = document.getElementById('show1');
        var square = document.getElementById('square');
        var enlarge = document.getElementById('enlarge');
        var where = document.getElementById('where');

        var midImg = document.getElementById('midImg');
        var bigImg = document.getElementById('bigImg');
        var main = document.getElementById('main');
        var smallImg = document.querySelectorAll('#smallImg li img');
        var smallLi = document.querySelectorAll('#smallImg li');
        // alert(smallLi.length)
        // alert(smallImg.length)

        smallImg[0].onmouseover = function () {
            enlarge.style.backgroundImage = 'url(./img/01.jpg)';
            midImg.src = "./img/01_mid.jpg";
            bigImg.src = "./img/01.jpg";
            // smallLi[0].style.border = '2px solid red';
        }

        smallImg[1].onmouseover = function () {
            enlarge.style.backgroundImage = 'url(./img/02.jpg)';
            midImg.src = "./img/02_mid.jpg";
            bigImg.src = "./img/02.jpg";
            // smallLi[1].style.borderColor = '2px solid red';

        }

        smallImg[2].onmouseover = function () {
            enlarge.style.backgroundImage = 'url(./img/03.jpg)';
            midImg.src = "./img/03_mid.jpg";
            bigImg.src = "./img/03.jpg";
            // smallLi[2].style.borderColor = '2px solid red';


        }

        smallImg[3].onmouseover = function () {
            enlarge.style.backgroundImage = 'url(./img/04.jpg)';
            midImg.src = "./img/04_mid.jpg";
            bigImg.src = "./img/04.jpg";

        }

        smallImg[4].onmouseover = function () {
            enlarge.style.backgroundImage = 'url(./img/05.jpg)';
            midImg.src = "./img/05_mid.jpg";
            bigImg.src = "./img/05.jpg";

        }

        show1.onmouseover = function () {
            // alert(square.offsetHeight)
            // square.style.width = a + 'px';
            // square.style.height = b + 'px';
            enlarge.style.display = 'block';
            square.style.display = 'block';
            //  midImg/bigImg=square/enlarge;

        }

        show1.onmouseout = function () {
            enlarge.style.display = 'none';
            square.style.display = 'none';
        }

        show1.onmousemove = function (e) {
            //clientWidth不帶邊框
            // alert(midImg.width)
            // alert(enlarge.offsetWidth)

            // 設置藍色小方塊的大小
            var n = enlarge.offsetWidth / (bigImg.width / midImg.width);
            square.style.width = n + 'px';
            square.style.height = n + 'px';

            // alert(square.style.width)
            var _event = e || window.event;
            var mouseLeft = _event.clientX;
            var mouseTop = _event.clientY;
            // console.log(mouseLeft+'---'+mouseTop)
            // alert(midImg.offsetLeft+'---'+show1.offsetLeft+'---'+main.offsetLeft)
            // alert(midImg.offsetTop+'---'+show1.offsetLeft+'---'+main.offsetLeft)
            // alert(midImg.offsetLeft)    //57px
            //mouseLeft             當前鼠標相對與瀏覽器x坐標
            //main.offsetLeft      最外邊框距瀏覽器左邊的length
            //show1.offsetLeft      圖片距外邊框左邊的length,為0
            //midImg.offsetLeft     里面圖片的padding-left
            var squareLeft = mouseLeft - show1.offsetLeft - main.offsetLeft - square.offsetWidth / 2;
            var squareTop = mouseTop - show1.offsetTop - main.offsetTop - square.offsetWidth / 2;
            // alert(square.offsetWidth)
            if (squareLeft >= midImg.width + midImg.offsetLeft - square.offsetWidth) {
                squareLeft = midImg.width + midImg.offsetLeft - square.offsetWidth;
            }

            // alert(main.offsetTop)
            if (squareTop >= midImg.height + midImg.offsetTop - square.offsetHeight) {
                squareTop = midImg.height + midImg.offsetTop - square.offsetHeight;
            }

            if (squareLeft < midImg.offsetLeft) {
                squareLeft = midImg.offsetLeft;
            }

            if (squareTop < midImg.offsetTop) {
                squareTop = midImg.offsetTop;
            }

            // alert(squareLeft+'=='+squareTop)

            square.style.left = squareLeft + 'px';
            square.style.top = squareTop + 'px';

            var scaleX = (squareLeft - midImg.offsetLeft) / midImg.width;
            var scaleY = (squareTop - midImg.offsetTop) / midImg.height;
            var enlargeLeft = scaleX * bigImg.width;
            var enlargeTop = scaleY * bigImg.height;

            enlarge.style.backgroundPosition = '-' + enlargeLeft + 'px -' + enlargeTop + 'px';

        }
    </script>
</body>

</html>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容