• 拖拽改变元素位置或大小


    <!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';
    }
    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 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 = oldH + newX;
    if (newW >= W) {
    newW = W;
    }
    if (newH >= H - oldT) {
    newH = H - oldT;
    }
    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 = oldW + newY;
    if (newW >= W-oldL) {
    newW = W - oldL;
    }
    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;
    var newH = oldH + newX;
    if (newW >= W-oldL) {
    newW = W - oldL;
    newH = W - oldL;
    }
    }
    if (obj1.className.indexOf('b') != -1) {
    var newY = oEvent.clientY - downY;
    var newH = oldH + newY;
    var newW = oldW + newY;
    if (newW >= W - oldL) {
    newW = W - oldL;
    }
    if (newH >= H - oldT) {
    newH = H - oldT;
    }
    }
    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>

  • 相关阅读:
    Sass安装与Webstorm File Watcher配置
    AngularJS ng-disabled在a内无效
    AngularJS 指令(Directivce )一
    Bus Hound 检测USB数据收发的教程
    STM32Cumebx HAL库实现STM32 USB虚拟串口的收发
    Docker学习笔记六:Docker搭建企业级私有仓库
    Docker学习笔记五:Docker生成jenkins容器,支持Java Web项目持续集成、持续部署
    Linux学习笔记一:Linux配置java环境变量
    Docker学习笔记四:Docker镜像、容器管理工具shipyard
    Redis学习笔记一:Redis安装
  • 原文地址:https://www.cnblogs.com/AlexandraZhang/p/6217090.html
Copyright © 2020-2023  润新知