js自由拼图

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

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>


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356