2018-12-01

放大镜通过图片比例的方式可以做到,可以用画布来绘出放大的图形,用它的drawImage()方法特定点开始绘制放大的图形,同时也可用背景图片的方法,不同在于要改变背景图片的位置。

放大镜方框的移动在原图形上,并且会随鼠标的移动而移动,鼠标始终处于放大镜的中心,用到clientX()clientY()与offsetWidth()offsetHeight()offsetLeft () offsetTop()来让鼠标处于放大镜的中心。





放大镜

 

   

   

 

 

 

     

     

     
    <

     
    >

     

     

     

     

    #demo{

    width: 2000px;

    height: 400px;

    }

    #vessal{

    float: left;

    position: relative;

    top: 10%;

    left: 20%;

    width: 400px;

    height: 400px;

    border: 3px solid #D3D9EB;

    background-repeat: no-repeat;

        background-size: 400px;

    }

    #vessal :hover{

    cursor:move;

    }

    #magnify{

    display: none;

    width: 400px;

    height: 400px;

    float: left;

    position: relative;

    top: 10%;

    left: 23%;

    border: 3px solid#EFC4C4;

    background-repeat: no-repeat;

    }

    #display{

    display: none;

    position: relative;

    width: 200px;

    height: 200px;

    /*border: 1px solid red;*/

    background-color: rgba(176,164,123,0.3);

    }

    #scroll{

        position: fixed;

    top:453px;

    left:392px;

    width: 430px;

    height: 65px;

    /*border: 1px solid red;*/

    overflow: hidden;

    }

    #scroll ul{

    position: absolute;

    display: inline;

    float: left;

    margin:0px 0px;

    width: 1290px;

    height: 60px;

    padding: 0px 0px;

    }

    #scroll ul li {

    float: left;

    display: inline;

    width: 80px;

    height: 60px;

    list-style: none;

    border: 3px solid #EBDEDE;

    border-radius: 3px;

    background-size: 80px 60px;

    background-repeat: no-repeat;

    }

    #scroll ul:hover{

    cursor:pointer ;

    }

    #rightbutton,#leftbutton{

    width: 40px;

        height: 60px;

        float: left;

        text-align: center;

        line-height: 60px;

        border-radius: 3px;

        background-color: rgba(129,182,243,0.8);

    }

    #rightbutton:hover{

    cursor:pointer ;

    }

    #leftbutton:hover{

    cursor:pointer ;

    }

    #leftbutton{

    position: fixed;

    left: 350px;

    top: 452px;

    }

    #rightbutton{

    position: fixed;

    left:820px;

    top: 452px;

    }

    #ratio{

      position: fixed;

      float: top;

      top: 50px;

      left: 812px;

    }

    #ratio button{

    margin:10px;

    display: block;

        background-color: rgba(105,125,125,0.5);

        border: 1px solid;

        border-radius: 3px;

    }

    window.onload=function () {

    var display=document.getElementById('display');

    var vessal=document.getElementById('vessal');

    var magnify=document.getElementById('magnify');

        var demo=document.getElementById('demo');

        var multi=800;

        var scroll=document.getElementById('scroll');

        var photoAray=document.getElementsByTagName('li')

        var object_ul=document.getElementsByTagName('ul')[0];

        var leftmove=document.getElementById('leftbutton');

        var rightmove=document.getElementById('rightbutton');

        var photoDistance=86;

        var moveDistance=86;

        var index=6;

        var bool=false;

        var animate=false;

        var twom=document.getElementById('twomultiple');

        var onem=document.getElementById('onemultiple');

        vessal.style.backgroundSize=vessal.offsetWidth+"px"+" "+vessal.offsetHeight+"px";

        vessal.onmouseover=function(){

        display.style.display="block";

        magnify.style.display="block";

        }

        vessal.onmouseout=function(){

        display.style.display="none";

        magnify.style.display="none";

        }

        vessal.onmousemove=function(event){

            var vess=event||window.event;

            var left=vess.clientX-vessal.offsetLeft-display.offsetWidth/2;

            var top=vess.clientY-vessal.offsetTop-display.offsetWidth/2;

            if (left<0) {

            left=0;

            }

            else if (left>(vessal.offsetWidth-display.offsetWidth)) {

            left=vessal.offsetWidth-display.offsetWidth;

            }

            if (top<0) {

            top=0;

            }

            else if (top>(vessal.offsetHeight-display.offsetHeight)) {

            top=vessal.offsetHeight-display.offsetHeight;

            }

            display.style.left=left+"px";

            display.style.top=top+"px";

            var posx=(left)/vessal.offsetWidth*(-multi);

            var posy=(top)/vessal.offsetHeight*(-multi);

            magnify.style.backgroundPosition=posx+"px"+" "+posy+"px";

        }

        function resetHtml(){


        var list="";

        for (var i = 0; i<15 ; i++) {

        list="
  • "+"
  • "+list;

        }

        object_ul.innerHTML=list;

        for (var i = 0; i < photoAray.length; i++) {

        var num=i+1;

        photoAray[i].style.background="url(../img/"+num+".jpg)";

        photoAray[i].setAttribute("index",num);

        photoAray[i].style.backgroundSize=80+"px"+" "+60+"px";

        }

        magnify.style.backgroundSize=multi+"px"+" "+multi+"px";

        onem.style.backgroundColor="#B42828";

        }


        function smooth(distance){

        var movetime=200;

            var time=2;

        var offset=distance/(movetime/time);  //每次移动距离;

        var offseted=parseFloat(object_ul.style.left)+distance;

        function goon () {

            if((parseFloat(object_ul.style.left)>offseted&&(distance<0))||(parseFloat(object_ul.style.left)0))) {

        animate=true;

        object_ul.style.left=parseFloat(object_ul.style.left)+ offset + "px";

        setTimeout(goon,time);

        }

        else

        { 

        animate=false;

    if (parseFloat(object_ul.style.left)<-774) {

        object_ul.style.left=-430+"px";

        }

        else if (parseFloat(object_ul.style.left)>-86) {

    object_ul.style.left=-430+"px";

    }

    }

    }

    goon();


        }

        rightmove.onclick=function(){   

            if (!animate) {

            index++;

            if (index>15) {

            index=1;

            }

            smooth(-86);}



        }

        leftmove.onclick=function(){

          if (!animate) {

            index--;

            if (index<1) {

            index=15;

            }

            smooth(86);

        }     

        }

        function rotation(){


            for (var i = 0; i < photoAray.length; i++) {

            photoAray[i].onmouseover=function(){

                    index=parseInt(this.getAttribute("index"));

                    vessal.style.backgroundImage="url(../img/"+index+".jpg)";

                    magnify.style.backgroundImage="url(../img/"+index+".jpg)";

                    for (var i = 0; i < photoAray.length; i++) {

                photoAray[i].style.border="3px solid #EBDEDE";

                    }

                    this.style.border="3px solid #8915F2";

            }

            }

            vessal.style.backgroundImage="url(../img/"+index+".jpg)";

            magnify.style.backgroundImage="url(../img/"+index+".jpg)";

        }

        twom.onclick=function(){

              multi=1600;

              twom.style.backgroundColor="#B42828";

              onem.style.backgroundColor="#ECB4B4";

              magnify.style.backgroundSize=multi+"px"+" "+multi+"px";

              magnify.style.width=multi/2+"px";

              magnify.style.height=multi/2+"px";

              magnify.style.top=0+"%";

        }

        onem.onclick=function(){

              multi=800;

              onem.style.backgroundColor="#B42828";

              twom.style.backgroundColor="#ECB4B4";

              magnify.style.backgroundSize=multi+"px"+" "+multi+"px";

              magnify.style.width=multi/2+"px";

              magnify.style.height=multi/2+"px";

              magnify.style.top=10+"%";

        }


        resetHtml();

        rotation();

    }




    你可能感兴趣的:(2018-12-01)