zoukankan      html  css  js  c++  java
  • 拖拽改变元素位置或大小bug修复

    <!doctype html>
    <html>
    <head>
       <meta charset="utf-8">
       <title>无标题文档</title>
    <style>
    body{
       background:#000;
    }
    .upshop-view{
       320px;
       height:499px;
       background:#fff;
       background-size:contain;
       position:relative;
       z-index:9998;
       margin:0 auto;

       overflow:hidden;

    }
    .touchmove{
       position:absolute;
       z-index:9990;
    }
    .touchmove span{
       display:block;
       10px;
       height:10px;
       background:#8F8F8F;
       position:absolute;
       display:none;
       z-index:9999;
    }
    .l{
       left:-5px;
       top:50%;
       margin-top:-5px;

       cursor:w-resize;

    }
    .r{
       right:-5px;
       top:50%;
       margin-top:-5px;

       cursor:e-resize;
    }
    .t{
       top:-5px;
       left:50%;
       margin-left:-5px;

       cursor:n-resize;
    }
    .b{
       bottom:-5px;
       left:50%;
       margin-left:-5px;

       cursor:s-resize;
    }
    .border{
        border:1px dashed #ccc;
    }
    .touchmove1{
        100px;
        height:100px;
        top:135px;
       left:110px;
    }
    .touchmove1 img{
       display:block;
       100%;
       height:100%;
       background:pink;
    }
    </style>
    <script src="jquery-1.7.2.js"></script>
    <script>
    $(function(){
    //拖拽图标
    var oUpshopView = document.getElementById('upshop_view');
    var oTouchmove1 = oUpshopView.getElementsByTagName('div')[0];
    var oImg1 = $('#touchmove1').children('img')[0];
    var aSpan1 = $('#touchmove1').children('span');

    ShowBox($('#touchmove1'));
    //拖拽改变图标大小
    for (var i = 0; i < aSpan1.length; i++) {
    DragSize(aSpan1[i], oTouchmove1, oUpshopView);
    }
    //拖拽改变图标位置
    DragPosition(oImg1,oTouchmove1, oUpshopView);
    });
    function ShowBox(obj) {
    obj.on('mouseenter', function () {
    $(this).children('span').show();
    $(this).addClass('border');
    });
    obj.on('mouseleave', function () {
    $(this).children('span').hide();
    $(this).removeClass('border');
    });
    }

    ///手机展示区域拖拽改变图标位置
    function getPos(obj) {
    var l = 0;
    var t = 0;
    while (obj) {
    l += obj.offsetLeft;
    t += obj.offsetTop;
    obj = obj.offsetParent;
    }
    return { left: l, top: t };
    }
    function DragPosition(obj1, obj2, obj3) {//obj1拖拽的元素,obj2改变的元素,obj3拖拽元素的父级用于限制拖拽区域;
    var W = obj3.offsetWidth;
    var H = obj3.offsetHeight;
    obj1.onmousedown = function (ev) {
    var oEvent = ev || event;
    var disX = oEvent.clientX - getPos(obj2).left;
    var disY = oEvent.clientY - getPos(obj2).top;
    document.onmousemove = function (ev) {
    var oEvent = ev || event;
    var l = oEvent.clientX - disX - getPos(obj3).left;
    var t = oEvent.clientY - disY - getPos(obj3).top;
    if (l <= 0) {
    l = 0;
    } else if (l >= W - obj2.offsetWidth) {
    l = W - obj2.offsetWidth;
    }
    if (t <= 0) {
    t = 0;
    } else if (t >= H - obj2.offsetHeight) {
    t = H - obj2.offsetHeight;
    }
    obj2.style.left = l + 'px';
    obj2.style.top = t + 'px';
    localStorage.left1 = l;
    localStorage.top1 = t;

    }
    document.onmouseup = function () {
    document.onmousemove = null;
    document.onmouseup = null;
    obj1.releaseCapture && obj1.releaseCapture();
    }
    obj1.setCapture && obj1.setCapture();
    return false;
    }

    }

    //手机展示区域拖拽改变图标大小
    function DragSize(obj1, obj2, obj3) {//obj1:拖拽的元素,obj2:改变的元素,obj3改变元素的父级用于限制拖拽区域;
    var W = obj3.offsetWidth;
    var H = obj3.offsetHeight;
    obj1.onmousedown = function (ev) {
    var oEvent = ev || event;
    var oldW = obj2.offsetWidth;
    var oldH = obj2.offsetHeight;
    var scale = 1;
    var oldL = obj2.offsetLeft;
    var oldT = obj2.offsetTop;
    var downX = oEvent.clientX;
    var downY = oEvent.clientY;
    document.onmousemove = function (ev) {
    var oEvent = ev || event;
    if (obj1.className.indexOf('l') != -1) {
    var newX = downX - oEvent.clientX;
    var newL = oldL - newX;
    var newW = oldW + newX;
    var newH = newW / scale;
    if (newW >= W) {
    newW = W;
    newH = newW / scale;
    }
    if (newH >= H - oldT) {
    newH = H - oldT;
    newW = newH * scale;
    }
    if (newL <= 0) {
    newL = 0;
    } else if (newL >= W - obj2.offsetWidth) {
    newL = W - obj2.offsetWidth;
    }
    obj2.style.left = newL + 'px';
    }
    if (obj1.className.indexOf('t') != -1) {
    var newY = downY - oEvent.clientY;
    var newT = oldT - newY;
    var newH = oldH + newY;
    var newW = newH * scale;
    if (newW >= W - oldL) {
    newW = W - oldL;
    newH = newW * scale;
    }
    if (newH >= H) {
    newH = H;
    }
    if (newT <= 0) {
    newT = 0;
    } else if (newT >= H - obj2.offsetHeight) {
    newT = H - obj2.offsetHeight;
    }
    obj2.style.top = newT + 'px';
    }
    if (obj1.className.indexOf('r') != -1) {
    var newX = oEvent.clientX - downX;
    var newW = oldW + newX;
    if (newW >= W - oldL) {
    newW = W - oldL;
    }
    var newH = newW / scale;
    if (newH >= H - oldT) {
    newH = H - oldT;
    newW = newH * scale;
    }
    }
    if (obj1.className.indexOf('b') != -1) {
    var newY = oEvent.clientY - downY;
    var newH = oldH + newY;
    var newW = newH * scale;
    if (newW >= W - oldL) {
    newW = W - oldL;
    newH = newW / scale;
    }
    if (newH >= H - oldT) {
    newH = H - oldT;
    newW = newH * scale;
    }
    }
    obj2.style.width = newW + 'px';
    obj2.style.height = newH + 'px';
    }
    document.onmouseup = function () {
    document.onmousemove = null;
    document.onmouseup = null;
    obj1.releaseCapture && obj1.releaseCapture();
    }
    obj1.setCapture && obj1.setCapture();
    return false;
    }
    }
    </script>
    </head>
    <body>
    <div class="upshop-view" id="upshop_view">
    <div class="touchmove touchmove1" id="touchmove1">
    <img src="../../Images/Common/headLogo.jpg"/>
    <span class="l"></span>
    <span class="r"></span>
    <span class="t"></span>
    <span class="b"></span>
    </div>
    <div>
    </body>
    </html>

  • 相关阅读:
    架构设计中的方法学——迭代设计
    架构重构改善既有代码的设计
    架构如何为业务和技术“服务”(2)
    架构如何为业务和技术“服务”(1)
    选用面向领域的多层分布式架构(DDD风格架构)的理由
    传话游戏(2013编程之美全国挑战赛资格赛)
    2012年蓝桥杯软件设计大赛河南省初赛试题和答案
    最大值最下化
    NYOJ120 校园网络(强连通缩点targan算法)
    NYOJ176整数划分(二)
  • 原文地址:https://www.cnblogs.com/AlexandraZhang/p/6381393.html
Copyright © 2011-2022 走看看