JavaScript之放大镜效果

在网上也浏览过许多关于JavaScript放大镜效果的文章,有的代码解释得些隐晦难懂,看的我头有点晕晕的╮(╯﹏╰)╭,我的心情是这样的:

JavaScript之放大镜效果

吐槽完了,我们动动小鼠标,当鼠标经过下面这张美女图片时就实现放大效果:

这个放大效果引用了MagicZoom.cssmz-packed.js

JavaScript之放大镜效果

方法一:

<!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/1999/xhtml">
 <head>
  <title>JavaScript之放大镜效果</title>
  <meta name="generator" content="editplus" charset="utf-8"/>
<link rel="stylesheet" href="http://files.cnblogs.com/files/caidupingblogs/MagicZoom.css" type="text/css" media="screen" />
<script src="http://files.cnblogs.com/files/caidupingblogs/mz-packed.js" type="text/javascript"></script>
 </head>

 <body>
  <script type="text/javascript" src="http://files.cnblogs.com/files/caidupingblogs/mz-packed.js"></script>
<p><a class="MagicZoom" title="MagicZoom: Super bike" href="http://images.cnblogs.com/cnblogs_com/caidupingblogs/828701/o_03.jpg"> <img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHAvaW1hZ2VzLmNuYmxvZ3MuY29tL2NuYmxvZ3NfY29tL2NhaWR1cGluZ2Jsb2dzLzgyODcwMS90XzAzLmpwZw==.jpg" alt="" /></a></p>
 </body>
</html

>

方法二:引入jquery-1.12.3.js

<!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/1999/xhtml">
 <head>
  <title> new document </title>
  <meta name="generator" content="editplus" charset="utf-8"/>
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
<script src="http://files.cnblogs.com/files/caidupingblogs/jquery-1.12.3.js" type="text/javascript"></script>
 <script type="text/javascript">
        $(function(){
            // 1.鼠标覆盖min 显示放大镜
            $('.min').mousemove(function(e) {
                $('.max').show()
                $('.enlarge').show()
                // 放大镜移动
                // pageX/Y 相对于body内容的x/y(滚动条变化时跟随变化)
                                // offset().left 相当于 offsetLeft
                var x = e.pageX - $('.min').offset().left - $('enlarge').width() / 2
                var y = e.pageY - $('.min').offset().top - $('.enlarge').height() / 2
                // 最大移动范围
                var maxX = $('.min').width() - $('.enlarge').width()
                var maxY = $('.min').height() - $('.enlarge').height()
                // 范围约束
                if (x <= 0) {
                    x = 0;
                } else if (x >= maxX) {
                    x = maxX;
                }
                if (y <= 0) {
                    y = 0;
                } else if (y >= maxY) {
                    y = maxY;
                }
                // 设置位置
                $('.enlarge').css({
                    left: x,
                    top: y
                })
                // 2.放大镜移动 max上的图片等比例移动
                var yidongX = x / maxX
                var yidongY = y / maxY
                $('.max>img').css({
                    left: -yidongX * ($('.max>img').width() - $('.max').width()),
                    top: -yidongY * ($('.max>img').height() - $('.max').height())
                })
            }).mouseout(function() {
                $('.max').hide()
                $('.fd').hide()
            })
        })
</script>
 <body>
   <body>
    <div class="min">
        <img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHAvaW1hZ2VzLmNuYmxvZ3MuY29tL2NuYmxvZ3NfY29tL2NhaWR1cGluZ2Jsb2dzLzgyODcwMS90XzAzLmpwZw==.jpg" />
        <div class="fd"></div>
    </div>
    <div class="max">
        <img src="http://images.cnblogs.com/cnblogs_com/caidupingblogs/828701/o_03.jpg"/>
    </div>
    </body>
    </body>
</html>
上一篇:Python函数中定义参数的四种方式 * **


下一篇:寒假:Day7