JQ的简易应用一则(放大)

若未使用JQ,请先配置jq环境
http://jquery.com/download/

简易放大镜功能

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
    #contain{
        position: absolute;
        width: 500px;
        height: 500px;
        border: 2px solid #000000;
    }
    .dd{
        position: absolute;
        width: 200px;
        height: 200px;
        float: left;
        border: 1px solid yellow;
    }
    img{
        width: 200px;
        height: 200px;
    }
    #div2{
        margin-left: 300px;
        margin-top:1px;
    }
    #div3{
        margin-left: 0px;
        margin-top:300px;
    }
    #div4{
        margin-left: 300px;
        margin-top:300px;
    }
    #big{
        position:absolute;
        width: 400px;
        height: 400px;
        background-color: green;
        display: none;
        border: 1px solid blue;
    }
    </style>
</head>
<body id="test">
    <div id="contain">
    <div class="dd" id="div1"><img src="gou.jpg"></div>
    <div class="dd" id="div2"><img src="niu.jpg"></div>
    <div class="dd" id="div3"><img src="she.jpg"></div>
    <div class="dd" id="div4"><img src="shu.jpg"></div>
    </div>
    <div id="big"></div>
<script type="text/javascript" src="../node_modules/jquery/dist/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#div1").mousemove(function(e) {
        var x = e.originalEvent.x || e.originalEvent.layerX || 0;
        var y = e.originalEvent.y || e.originalEvent.layerY || 0;
        var xx=x+300;
        var yy=y+50;
            $('#big').css({'background-image':'url(gou.jpg)','background-size':'100% 100%','display':'block'});
             $('#big').css('marginLeft',xx);
             $('#big').css('marginTop',yy);
        })
        $("#div1").mouseout(function() {
             $('#big').css({'display':'none'});
        })
        $("#div2").mousemove(function(e) {
        var x = e.originalEvent.x || e.originalEvent.layerX || 0;
        var y = e.originalEvent.y || e.originalEvent.layerY || 0;
        var xx=x+300;
        var yy=y+50;
            $('#big').css({'background-image':'url(niu.jpg)','background-size':'100% 100%','display':'block'});
             $('#big').css('marginLeft',xx);
             $('#big').css('marginTop',yy);
        })
        $("#div2").mouseout(function() {
             $('#big').css({'display':'none'});
        })
        $("#div3").mousemove(function(e) {
        var x = e.originalEvent.x || e.originalEvent.layerX || 0;
        var y = e.originalEvent.y || e.originalEvent.layerY || 0;
        var xx=x+300;
        var yy=y-400;
            $('#big').css({'background-image':'url(she.jpg)','background-size':'100% 100%','display':'block'});
             $('#big').css('marginLeft',xx);
             $('#big').css('marginTop',yy);
        })
        $("#div3").mouseout(function() {
             $('#big').css({'display':'none'});
        })
        $("#div4").mousemove(function(e) {
        var x = e.originalEvent.x || e.originalEvent.layerX || 0;
        var y = e.originalEvent.y || e.originalEvent.layerY || 0;
        var xx=x+300;
        var yy=y-400;
            $('#big').css({'background-image':'url(shu.jpg)','background-size':'100% 100%','display':'block'});
             $('#big').css('marginLeft',xx);
             $('#big').css('marginTop',yy);
        })
        $("#div4").mouseout(function() {
             $('#big').css({'display':'none'});
        })
        })
</script>
</body>
</html>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的使用jQuery实现二维码放大的示例代码: HTML代码: ``` <div class="qrcode"> <img src="二维码地址" alt="二维码" /> </div> ``` CSS代码: ``` .qrcode { position: relative; } .qrcode img { display: block; width: 100%; height: auto; } .qrcode .popup { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: none; z-index: 999; } .qrcode .popup img { display: block; max-width: 300px; max-height: 300px; } ``` JavaScript代码: ``` $(document).ready(function() { $('.qrcode').hover(function() { var src = $(this).find('img').attr('src'); $(this).append('<div class="popup"><img src="' + src + '"/></div>'); $(this).find('.popup').fadeIn(200); }, function() { $(this).find('.popup').fadeOut(200, function() { $(this).remove(); }); }); }); ``` 解释一下代码:首先,HTML代码中有一个包含二维码图片的div元素。CSS代码中,我们将该元素设置为相对定位,二维码图片设置为100%的宽度和自适应的高度。我们还定义了一个名为“popup”的绝对定位元素,用于展示放大后的二维码图片,初始状态设置为不显示。JavaScript代码中,我们使用jQuery的hover方法监听该元素的鼠标进入和离开事件,当鼠标进入时,我们将该元素内部加入一个.popup元素,并将其内部的img元素的src属性设置为鼠标悬停在的二维码图片的地址,然后使用fadeIn方法显示.popup元素;当鼠标离开时,我们使用fadeOut方法隐藏.popup元素,完成隐藏后再将.popup元素从该元素中移除。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值