美文网首页
js自由拼图

js自由拼图

作者: galenv | 来源:发表于2020-07-07 15:29 被阅读0次

自由拖拽,鼠标滚轴缩放缩小

html 部分

<!DOCTYPE html>

<html lang="en">

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document

    li {

list-style:none;

    }

.meaasge_contanier {

width:800px;

        height:600px;

        margin:100px auto 0;

        background-color:sienna;

        position:relative;

        overflow:hidden;

        border:3px solid #a25124;

        border-radius:10px;

        box-shadow:3px 3px 5px #a25124;

    }

.meaasge_contanier .upload {

width:120px;

        height:40px;

        position:absolute;

        top:5px;

        left:5px;

        text-align:center;

    }

.meaasge_contanier .upload .plus {

width:100%;

        height:100%;

        text-align:center;

        line-height:40px;

        position:absolute;

        background-color:#eee;

        cursor:pointer;

        border-radius:5px;

    }

.meaasge_contanier .upload input {

width:150px;

        overflow:hidden;

    }

.meaasge_contanier .picture_list .item {

width:100px;

        position:absolute;

        box-shadow:2px 2px 3px rgba(0, 0, 0, 0.3);

        cursor:move;

    }

.meaasge_contanier .picture_list .item img {

width:100%;

    }

.meaasge_contanier .picture_list .item .delete {

position:absolute;

        width:20px;

        height:20px;

        line-height:17px;

        text-align:center;

        border-radius:50%;

        background-color:#909399;

        color:#fff;

        font-size:12px;

        top: -5px;

        right: -5px;

        display:none;

        cursor:default;

    }

.scale {

width:10px;

    height:10px;

    overflow:hidden;

    cursor:se-resize;

    position:absolute;

    right:0;

    bottom:0;

    background-color:rgb(122, 191, 238);

}

<div class="meaasge_contanier">

    <ul class="picture_list">

        <li class="item" data-type="0"><div class="delete">x</div><img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3888642441,3149824551&fm=26&gp=0.jpg" /><div class="scale">

        <li class="item" data-type="0"><div class="delete">x</div><img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2814303155,1069456303&fm=26&gp=0.jpg" /><div class="scale">

        <li class="item" data-type="1"><div class="delete">x

            <div class="fonts" >12345671111111111111

1111189

            <div class="scale">

</body>

</html>

javascript部分

<script src="js/jquery-3.4.1.min.js">

<script type="text/javascript">

    $(function () {

let pictureList =$('.picture_list')

let contanier =$('.meaasge_contanier')

pictureList.on('mousedown', '.item', function (e) {

e.preventDefault()

// 让点击的图片在第一层级

            let arr = Array.from($('.item'))

arr.forEach(item => {

$(item).css({

zIndex:0

                })

})

let item =$(this)

$(item).css({

zIndex:99

            })

const disX = e.clientX - item[0].offsetLeft

            const disY = e.clientY - item[0].offsetTop

            contanier.mousemove(function (event) {

event.preventDefault()

// 用移动时的位置的clientX减去初始的差值,就得到现在的top值与left值

                let x = event.clientX - disX

let y = event.clientY - disY

x = x <0 ?0 : x

x = x > contanier.width() - item.width() ? contanier.width() - item.width() : x

y = y <0 ?0 : y

y = y > contanier.height() - item.height() ? contanier.height() - item.height() : y

item.css({

top: y,

                    left: x

});

            })

contanier.mouseout(function () {

contanier.off('mousemove')

contanier.off('mouseup')

})

contanier.mouseup(function () {

contanier.off('mousemove')

contanier.off('mouseup')

})

})

pictureList.on('click', '.delete', function () {

$(this).parent('.item').remove()

})

pictureList.on('mouseover', '.item', function () {

$(this).children('.delete').show()

})

pictureList.on('mouseout', '.item', function () {

$(this).children('.delete').hide()

})

pictureList.on('mousedown', '.scale', function (e) {

e.stopPropagation();

            e.preventDefault();

            var item=$(this).parent();

            var pos = {

'w':$(item).width(),

                'h':$(item).height(),

                'x': e.clientX,

                'y': e.clientY

            };

            contanier.bind("mousemove",function (ev) {

ev.preventDefault()

// 设置图片的最小缩放为30*30

                var w = Math.max(30, ev.clientX - pos.x + pos.w);

                var h = Math.max(30,ev.clientY - pos.y + pos.h);

                // 设置图片的最大宽高

                w = w >= contanier[0].offsetWidth-item.offsetLeft ? contanier[0].offsetWidth-item.offsetLeft : w

h = h >= contanier[0].offsetHeight-item.offsetTop ? contanier[0].offsetHeight-item.offsetTop : h

item.css("width",w+"px");

                item.css("height",h+"px");

            });

        });

        pictureList.on("mousewheel DOMMouseScroll" ,'.item', function (e) {

let delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta >0 ?1 : -1)) ||// chrome & ie

                (e.originalEvent.detail && (e.originalEvent.detail >0 ? -1 :1));              // firefox

            let type=$(this).attr("data-type");

            let w=$(this).width();

            let h=$(this).height();

            if (delta >0) {

//放大图片

// 设置图片的最小缩放为30*30

                if(w>=contanier[0].offsetWidth||h>=contanier[0].offsetHeight)

{

return;

                }

if(type==0)

{

w +=30;

                    h+=30;

                    $(this).css("width",w+"px");

                    $(this).css("height",h+"px");

                }

else

                {

let fontsize=parseInt($(this).find(".fonts").css("font-size").replace("px",""));

                    if(fontsize>=48)return;

                    fontsize+=2;

                    $(this).find(".fonts").css("font-size",fontsize+"px");

                    $(this).css({"width":"auto","height":"auto"});

                }

}else if (delta <0) {

//缩小图片

                console.log(type);

                if(type==0)

{

// 设置图片的最小缩放为30*30

                    if(w<=100||h<=100)

{

return;

                    }

w -=30;

                    h -=30;

                    $(this).css("width",w+"px");

                    $(this).css("height",h+"px");

                }

else

                {

let fontsize=parseInt($(this).find(".fonts").css("font-size").replace("px",""));

                    if(fontsize<=16)

{

return;

                    }

console.log(fontsize);

                    fontsize-=2;

                    $(this).find(".fonts").css("font-size",fontsize+"px");

                    $(this).css({"width":"auto","height":"auto"});

                }

}

});

    });

</script>

相关文章

  • js自由拼图

    自由拖拽,鼠标滚轴缩放缩小 html部分 Document ...

  • canvas拼图效果

    效果图 vue文件代码 puzzle.js文件封装拼图对象

  • 拼图小游戏的简单制作

    闲暇时光,有人会用拼图、扫雷等等小游戏来打发时间,今天我就做了一下拼图小游戏,下面分享一下写js的过程和一点心得。...

  • 用代码就能制作游戏

    利用jQuery实现拼图游戏: 代码结构 1. 引入CSS 1. 2. 引入JS 3. HTML代码 开始 简...

  • 我们一家都玩拼图

    我玩拼图 画画的拼图 老弟玩水果拼图 妈妈玩文字拼图 爸爸玩图片拼图 爷爷玩瓷砖拼图 我们一家都玩拼图

  • js 滑动拼图验证码

    以前的验证码很简单,就是一个带些背景色或背景图和干扰线的纯数字字母类的验证码,现在已经发展变得很丰富了。我见过的就...

  • 2019-10-19

    我的小管家JS 周五下午,快到放学时间了,可是跟孩子们练习拼图中最后一副还剩几片没有拼完,拜托JS帮忙一起完成,因...

  • 人体拼图

    【2021.9.8】慢活30天,挑战任务:Do a puzzle(玩拼图)【没有拼图,你有拼图吗??】不如找个拼图...

  • js拼图思路(3X3)

    1.要实现的内容: 图片上传 切割图片 移动文件选择图标和游戏图片框 打乱图片存储的顺序 移动乱序的图片 交换要移...

  • 永远不要低估孩子的学习力

    娃最近对拼图感兴趣,继动物拼图后,我趁热打铁给入手了地图拼图。于是家里的拼图军团有了七巧板、俄罗斯方块、磁力片拼图...

网友评论

      本文标题:js自由拼图

      本文链接:https://www.haomeiwen.com/subject/bejfshtx.html