網頁常用動態效果--放大鏡

來源:互聯網
上載者:User

標籤:

HTML代碼:

div.box>span+div

CSS代碼:

box相對定位,span在box內絕對位置,div絕對位置超出其自身寬

.box{            width:350px;            height:350px;            background: url(images/xiaotu.jpg) 0 0 no-repeat;            margin:100px;            border:1px solid #000;            position: relative;        }        .box span{            width:150px;            height:150px;            background: red;            position: absolute;            left:0;            top:0;            opacity:0.3;            cursor:move;            display:none;        }        .box div{            width:400px;            height:400px;            background: url(images/datu.jpg) 0 0 no-repeat;            border:1px solid #000;            position: absolute;            right:-420px;            top:0;            display:none;        }

JQ代碼:

 <script>            //將span限制在box中拖拽,同時根據span拖拽的座標來對應div大圖中的座標            $(function(){                var divX = $(‘.box‘).offset().left;                var divY = $(‘.box‘).offset().top;                var w1 = $(‘span‘).width();                var h1 = $(‘span‘).height();                var w2 = $(‘.box‘).width();                var h2 = $(‘.box‘).height();                 $(‘.box‘).mouseover(function(event) {                    $(‘.box span,.box div‘).show();                    //滑鼠移動                    $(‘.box‘).mousemove(function(event) {                        //擷取滑鼠座標                        var mx = event.pageX;                        var my = event.pageY;                        //拖拽點                        var x = mx-divX-75;                        var y = my-divY-75;                        if(x<0){                            x=0;                        }                        if(y<0){                            y=0;                        }                        if(x>w2-w1){                            x=w2-w1;                        }                        if(y>h2-h1){                            y=h2-h1;                        }                        //對應div背景的座標                        var posX = 800*x/350;                        var posY = 800*y/350;                        $(‘span‘).css({left:x,top:y});                        $(‘.box div‘).css({                            backgroundPosition:(-posX)+‘px ‘+(-posY)+‘px‘                        });                    });                });                //離開box後,事件消失                $(‘.box‘).mouseout(function(event) {                    $(‘.box span,.box div‘).hide();                });            })        </script>

 

網頁常用動態效果--放大鏡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.