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中超出盒子宽度会自动换行的,不用设置,如果你连续的英文之间没用空格,
        那么它会以为是一个单词,就不会换行,这个时候加上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() 方法能够通过参数接收无限数量的新元素。可以通过 jQuery 来生成文本/HTML(就像上面的例子那样),或者通过 JavaScript
                代码和 DOM 元素。 在下面的例子中,我们创建若干个新元素。这些元素可以通过 text/HTML、jQuery 或者 JavaScript/DOM 来创建。然后我们通过 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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容