jq编写放大镜


        <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
</head>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<style>
    .box{
        display: flex;
    }
    .small img {
        width: 50px;
        height: 50px;
        display: block;
        border: 1px solid #555;
        margin: 10px;
        cursor: pointer;
    }

    .small .active {
        border-color: #e77600;
        box-shadow: 0 0 3px 2px rgba(228, 121, 17, 0.5);
    }

    .middle {
        width: 500px;
        height: 500px;
        position: relative;
        margin: 10px;
    }

    .middle img {
        width: 500px;
        height: 500px;
    }

    .middle .mask {
        width: 100px;
        height: 100px;
        background-color: rgba(0, 100, 200, 0.3);
        position: absolute;
        top: 0;
        left: 0;
        display: none;
        cursor: move;
    }

    .middle .big {
        width: 400px;
        height: 400px;
        position: absolute;
        left: 105%;
        top: 0;
        overflow: hidden;
        display: none;
    }

    .middle .big img {
        width: 2000px;
        height: 2000px;
        position: absolute;
        left: 0;
        top: 0;
    }
</style>

<body>
    <div class="box">
        <div class="small">
            <img class="active" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F07%2F20200407001816_sxbcg.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657885333&t=c54acb6253ad1b9a625dbfa2031c1ddf" alt="">
            <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202102%2F24%2F20210224222557_a57ac.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657885333&t=2ebffd4cb494e46ea92563d34bc6fed4" alt="">
            <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201901%2F12%2F20190112235134_RTyKN.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657885423&t=f15684d1ea95ce24f6dc6b27f842d66f" alt="">
            <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F09%2F20200409085108_dshqx.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657885475&t=615a7d670e161e69fb72331389e3db9e" alt="">
            <img src="https://img2.baidu.com/it/u=518940714,1656717865&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=454" alt="">
            <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.15lu.com%2Fdedecms%2Fuploads%2Fallimg%2F190310%2F3-160526104330263.jpg&refer=http%3A%2F%2Fwww.15lu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657885565&t=dd63b3acd33824e9aa646e152a551311" alt="">
        </div>
        <div class="middle">
            <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F07%2F20200407001816_sxbcg.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657885333&t=c54acb6253ad1b9a625dbfa2031c1ddf">
            <div class="mask"></div>
            <div class="big">
                <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F07%2F20200407001816_sxbcg.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657885333&t=c54acb6253ad1b9a625dbfa2031c1ddf">
            </div>
        </div>
    </div>
    <script>
         
        $('.small img').click(function () {
            //类名设置
            $(this).addClass('active').siblings().removeClass('active')
            // 小图与中图的地址,链接
            $('.middle>img').attr('src', $(this).attr('src'))
            // 大图与中图的地址,链接
            $('.big>img').attr('src', $(this).attr('src'))
        })
        //鼠标移入显示隐藏
        $('.middle').hover(function () {
            $('.big').show()
            $('.mask').show()

            //移动事件
            $('.middle').mousemove(function (e) {
                //获取鼠标位置
                var x = e.pageX
                var y = e.pageY
                var l = x - $('.mask').outerWidth() / 2
                var t = y - $('.mask').outerHeight() / 2
                //对l,t的限制
                if (l < $('.middle').offset().left + parseInt($('.middle').css('border-left-width'))) {
                    l = $('.middle').offset().left + parseInt($('.middle').css('border-left-width'))
                }
                if (t < $('.middle').offset().top + parseInt($('.middle').css('border-top-width'))) {
                    t = $('.middle').offset().top + parseInt($('.middle').css('border-top-width'))
                }
                if (l > $('.middle').offset().left + parseInt($('.middle').css('border-left-width')) + $(
                        '.middle').innerWidth() - $('.mask').outerWidth()) {
                    l = $('.middle').offset().left + parseInt($('.middle').css('border-left-width')) + $(
                        '.middle').innerWidth() - $('.mask').outerWidth()
                }
                if (t > $('.middle').offset().top + parseInt($('.middle').css('border-top-width')) + $(
                        '.middle').innerHeight() - $('.mask').outerHeight()) {
                    t = $('.middle').offset().top + parseInt($('.middle').css('border-top-width')) + $(
                        '.middle').innerHeight() - $('.mask').outerHeight()
                }
                //设置遮罩移动位置
                $('.mask').offset({
                    left: l,
                    top: t
                })
                //获取遮罩相对于中盒子的距离
                var l1 = $('.mask').position().left / $('.middle').innerWidth() * $('.big img').width()
                var t1 = $('.mask').position().top / $('.middle').innerHeight() * $('.big img').height()
                $('.big>img').css({
                    left: -l1 + 'px',
                    top: -t1 + 'px'
                })
            })
        }, function () {
            $('.big').hide()
            $('.mask').hide()
        })
    

    </script>

</html>
  

假设你有一个商品列表,每个商品都有名称、价格和库存数量。你可以使用 jq 编写一个简单的购物车程序来处理用户的购买请求。以下是一个可能的实现: ```bash # 商品列表,格式为 JSON 数组 products='[ {"name": "apple", "price": 2.5, "quantity": 10}, {"name": "banana", "price": 1.5, "quantity": 5}, {"name": "orange", "price": 3, "quantity": 8}, {"name": "pear", "price": 2, "quantity": 12} ]' # 定义一个空的购物车,格式为 JSON 对象 cart='{"items": []}' # 处理用户输入,假设用户输入的是商品名称和购买数量,格式为 "name:quantity" input="apple:3" # 解析用户输入,获取商品名称和购买数量 name=$(echo "$input" | cut -d':' -f1) quantity=$(echo "$input" | cut -d':' -f2) # 查找商品列表中对应的商品 product=$(echo "$products" | jq ".[] | select(.name == \"$name\")") # 检查商品是否存在以及库存是否足够 if [ -z "$product" ]; then echo "Product not found" elif [ "$(echo "$product" | jq '.quantity')" -lt "$quantity" ]; then echo "Not enough stock" else # 将商品添加到购物车中 item=$(echo "$product" | jq ".quantity = $quantity") cart=$(echo "$cart" | jq ".items += [$item]") echo "Item added to cart" fi # 打印购物车内容 echo "$cart" | jq ``` 这个程序使用 jq 命令来解析和操作 JSON 数据。首先定义了商品列表和购物车的初始值,然后处理用户输入,并在商品列表中查找对应的商品。如果商品存在且库存足够,将商品添加到购物车中。最后打印购物车内容。你可以根据需要扩展这个程序,例如支持删除购物车中的商品、计算购物车总价等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值