商品放大镜的效果的实现!

需求:商品展示图片的放大效果,

运用技术:onmousemove,event事件,event.clientX,event.clientY,简单的实现商品放大效果,

tips:可以下载源码直接放到编辑器里面看效果,请注意需要往class为img的img标签里添加图片地址,这样就可以实现一个简单的放大镜效果了,

源码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box{
                width:800px;
                height:600px;
                border:1px solid #000;
                position:relative;
                float:left;

                margin:0 50px;
            }
            img{
                width:800px;
                height:600px;
            }
            .box div{
                width:200px;
                height:200px;
                background-color: rgba(225,225,225,.5);
                position:absolute;
                left:0;
                right:0;
                display: none;
                overflow: hidden;
                border-radius: 100px;
                box-shadow: 0px 0px 5px #000;
                border:1px solid #000;
                cursor: pointer;
            }
            .showBox{
                width:400px;
                height:400px;
                border:1px solid #000;
                float: left;
                margin-top:150px;
                position: relative;
                overflow: hidden;
                border-radius: 200px;
            }
            .move{
                position:absolute;
                left:0;
                top:0;
            }
            .foloow{

                position:absolute;
                top:0;
                left:0;
            }
        </style>
    </head>
    <body>
         <div class="box">
             <img src="img/5.jpg" alt="" class = "img"/>
             <div>
                 <img src="" alt="" class="foloow" />
             </div>
         </div>
         <div class="showBox"><img src="" alt="" class ="move"/></div>
    </body>
    <script type="text/javascript">
        //     获取元素,大盒子
        var box = document.querySelector(‘.box‘);
        // 获取放大镜
        var div = document.querySelector(‘.box div‘);
        // 获取显示放大镜的盒子
        var showBox = document.querySelector(‘.showBox‘);
        // 获取放大镜内部的图片
        var floow = document.querySelector(‘.foloow‘);
        // 获取需要移动的放大镜盒子里面的图片
        var move = document.querySelector(‘.move‘);
        var img = document.querySelector(‘.img‘);
        // 获取盒子内部的图片地址
        var src = img.src;
        box.onmousedown = function(ev){
            ev.preventDefault();
        }
        box.onmousemove = function(ev){
            ev.preventDefault();
            // 放大镜显示
            div.style.display = ‘block‘;
            // 获取光标x轴位置
            var x = ev.clientX;
            // 获取光标Y轴位置
            var y = ev.clientY;
             // 获取光标X轴需要移动的距离
            var l = x - box.offsetLeft - div.offsetWidth/2 - this.clientLeft;
            // 获取光标Y轴需要移动的距离
            var t = y - box.offsetTop - div.offsetHeight/2 - this.clientTop;
            // 左边缘处理
            if(l<=0){

                l=0;

            }
            //右边缘处理
            if(l>=box.clientWidth-div.offsetWidth){

                l = box.clientWidth-div.offsetWidth;

            }
            // 上边缘处理
            if(t<=0){
                t=0;
            }
            // 下边缘处理
            if(t>=box.clientHeight-div.offsetHeight){

                t = box.clientHeight-div.offsetHeight;

            }
            // 动态调整放大镜内部图片的尺寸以及地址
            floow.src = src;
            floow.style.width = box.offsetWidth+‘px‘;

            floow.style.height = box.offsetHeight +‘px‘;
            // 延时定时器突出放大镜效果
            setTimeout(function(){
                // 放大镜内部图片移动距离
                floow.style.left = -l +‘px‘;

                floow.style.top = -t +‘px‘;
            },20);

            // 光标移动距离
            div.style.left = l + ‘px‘;

            div.style.top =  t + ‘px‘;
            // 动态调整放大镜显示框的地址以及储存
            move.src = src;
            // 获取放大镜框与放大镜之间的比例 显示放大镜盒子的边长X4 除以放大镜盒子的边长X4;

            var num = ((showBox.clientHeight*2)+(showBox.clientWidth*2))/((div.clientHeight*2)+(div.clientWidth*2));

            move.style.width = num*box.clientWidth +‘px‘;// 动态设置放大镜盒子内部图片的大小

            move.style.height = num*box.clientHeight+‘px‘;// 动态设置放大镜盒子内部图片的大小

            move.style.left = -l*num+‘px‘;// 移动放大镜盒子里面图片的位置

            move.style.top = -t*num+‘px‘;// 移动放大镜盒子里面图片的位置 

        }
        box.onmouseout = function(){

            div.style.display = ‘none‘;

            move.src = ‘‘;

        }
    </script>
</html>
时间: 2024-08-03 01:45:43

商品放大镜的效果的实现!的相关文章

利用JS实现购物网站商品放大镜效果

大家几乎都有在网上购物的经验,有的网站会有一个商品放大镜功能, 就是把鼠标移到图片上的时候,旁边会有另外一张大的图片展示,等同于 放大镜效果,那这样的效果怎样实现的呢,我把代码发给大家,请大家参考. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>放大镜</title> <style> *{m

用js实现放大镜的效果

第一次发博客,还有点小激动,本人现在正在天津上大学,希望以后从事前端这一行业,学习的时间不长,写博客为了记录自己的学习过程和收获,当然也算是巩固.可能写的东西不会像大牛那样高大上,只是一些基本的内容,当然我也会收藏一些我认为好的,不错的文章(其实最主要是我能看懂....).我相信以后自己也能够写出高大上的东西.加油! 废话说的有点多,说正事.前几天看了一段潭州教育的教学视频,其中的老师是用JQuery实现放大镜的效果(老师讲的是在是太慢,各种乱扯,跳着看的).由于我还没有好好的学习过JQuery

商品图片局部放大效果

还是先看效果图,再晒源代码 当鼠标放到图片的哪个部位的时候,那个部位就会放大 就像淘宝网查看商品那样的效果 下面来看源代码 html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org

Salesforce 开发整理(十一) 自定义放大镜查找效果

有时候在自定义的visualforce页面上,需要实现系统标准的查找样式,当不能使用标准的style的时候,我们只能选择自定义实现,下面分享一个demo,预览效果如下: 实现代码,Visualforce页面 1 <!-- 自定义放大镜查找效果 --> 2 <apex:page showHeader="false" controller="SelectSystem" standardStylesheets="false" side

商品放大镜效果

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ padding:0px;margin:0px; } .smallpic{ width: 450px; height: 450px; border: 1px solid #ccc; positio

放大镜的效果显示

<!DOCTYPE html><html><head>    <title>放大镜效果显示</title>    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">    <script type="text/javascript" src="https://code.jquer

用JS实线放大镜的效果

HTML代码如下: <div id="preview"> <div id="mediumDiv"> <img id="mImg" src="images/products/product-s1-m.jpg"/>//这是中级图片层 <div id="mask"></div>//这是遮罩 <div id="superMask"

商品放大镜案例

index.html: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X

购物车商品加减效果

思路: 根据点击运算符进行相关的运算. 1.获取点击的运算符: 2.通过节点关系找到input的值: 3.进行运算: 4.将计算后的值返回给本身 JavaScript方式: <body> <button onclick="calculate(this)">-</button> <input type="text" size="2" value="1" name="num&qu