简体   繁体   English

使用CSS在div周围自定义叠加层-jQuery可调整大小的自定义句柄

[英]Custom overlay around a div using css - Custom handle for jQuery resizable

Hi i am using jquery resizable, rotatable() [ https://cdn.jsdelivr.net/gh/godswearhats/jquery-ui-rotatable@1.1/jquery.ui.rotatable.min.js] in my project . 嗨,我在我的项目中使用可调整大小的jquery,可旋转()[ https://cdn.jsdelivr.net/gh/godswearhats/jquery-ui-rotatable@1.1/jquery.ui.rotatable.min.js]

So when this function called i get handles to rotate and resize the div. 因此,当调用此函数时,我将获得旋转和调整div大小的手柄。

 $( function() { var inputLocalFont = document.getElementById("user_file"); inputLocalFont.addEventListener("change",previewImages,false); function previewImages(){ var fileList = this.files; var anyWindow = window.URL || window.webkitURL; for(var i = 0; i < fileList.length; i++){ var objectUrl = anyWindow.createObjectURL(fileList[i]); $('.new-multiple').append('<div class="img-div"><img src="' + objectUrl + '" class="newly-added" /></div>'); window.URL.revokeObjectURL(fileList[i]); } $( ".img-div" ).draggable(); $('.img-div').rotatable(); $( ".img-div" ).resizable({aspectRatio:true}); $(".newly-added").on("click", function(e) { $(".newly-added").removeClass("img-selected"); $(this).addClass("img-selected"); e.stopPropagation() }); $(document).on("click", function(e) { if ($(e.target).is(".newly-added") === false) { $(".newly-added").removeClass("img-selected"); } }); } }); 
 .new-multiple{ width:400px !important; height:400px !important; background:white; border:2px solid red; overflow:hidden; } .img-div{ width:200px; height:200px; } .newly-added{ width:100%; height:100%; } .img-selected{ box-shadow: 1px 2px 6px 6px rgb(206, 206, 206); border:2px solid rgb(145, 44, 94); } .ui-resizable-handle.ui-resizable-se.ui-icon.ui-icon-gripsmall-diagonal-se { background-color: white; border: 1px solid tomato; } .ui-rotatable-handle.ui-draggable { background-color: white !important; border: 1px solid tomato; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/godswearhats/jquery-ui-rotatable@1.1/jquery.ui.rotatable.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script src="https://cdn.jsdelivr.net/gh/godswearhats/jquery-ui-rotatable@1.1/jquery.ui.rotatable.min.js"></script> <input name="user_file[]" id="user_file" style="position: relative;overflow: hidden" multiple="" type="file"> <div class="new-multiple"></div> 

https://jsfiddle.net/vd11qyzv/21/ https://jsfiddle.net/vd11qyzv/21/

So when i am uploading a image i get control handles like this 所以当我上传图片时,我得到了这样的控制手柄

在此处输入图片说明

But i want to place custom control handle like this 但是我想像这样放置自定义控件句柄

在此处输入图片说明

How can i achieve this using css and jquery. 我如何使用css和jquery实现此目的。 Please help 请帮忙

When you set the handles option in resizable, you can then manage them with CSS. 在可调整大小的情况下设置handles选项时,可以使用CSS对其进行管理。 Their style and location. 他们的风格和位置。

CSS Snippet CSS片段

.ui-resizable-handle {
  border: 0;
  border-radius: 50%;
  background-color: #00CCff;
  width: 14px;
  height: 14px;
}

.ui-resizable-nw {
  top: -7px;
  left: -7px;
}

.ui-resizable-ne {
  top: -7px;
  right: -7px;
}

.ui-resizable-e {
  top: calc(50% - 7px);
  right: -7px;
}

.ui-resizable-w {
  top: calc(50% - 7px);
  left: -7px;
}

.ui-resizable-sw {
  bottom: -7px;
  left: -7px;
}

.ui-resizable-se {
  right: -7px;
  bottom: -7px;
}

.ui-resizable-se.ui-icon {
  display: none;
}

.ui-rotatable-handle {
  background-size: 14px;
  background-repeat: no-repeat;
  background-position: center;
  border: 0;
  border-radius: 50%;
  background-color: #00CCff;
  margin-left: calc(50% - 9px);
  bottom: -5px;
  width: 18px;
  height: 18px;
}

JavaScript JavaScript的

$(function() {
  var inputLocalFont = $("#user_file");
  inputLocalFont.change(previewImages);

  function previewImages() {
    var fileList = this.files;
    var anyWindow = window.URL || window.webkitURL;

    for (var i = 0; i < fileList.length; i++) {
      var objectUrl = anyWindow.createObjectURL(fileList[i]);
      var $newDiv = $("<div>", {
        class: "img-div"
      });
      var $newImg = $("<img>", {
        src: objectUrl,
        class: "newly-added"
      }).appendTo($newDiv);
      $(".new-multiple").append($newDiv);
      $newDiv.draggable();
      $newDiv.rotatable();
      $newDiv.resizable({
        aspectRatio: true,
        handles: "ne, nw, e, se, sw, w"
      });
      $newDiv.find(".ui-icon").removeClass("ui-icon ui-icon-gripsmall-diagonal-se");
      window.URL.revokeObjectURL(fileList[i]);
    }
    $(".newly-added").on("click", function(e) {
      $(".newly-added").removeClass("img-selected");
      $(this).addClass("img-selected");
      e.stopPropagation()
    });

    $(document).on("click", function(e) {
      if ($(e.target).is(".newly-added") === false) {
        $(".newly-added").removeClass("img-selected");
      }
    });
  }
});

Working Example: https://jsfiddle.net/Twisty/s99kxydw/ 工作示例: https : //jsfiddle.net/Twisty/s99kxydw/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM