javascript - 如何使用css和js为div中的每个图像制作图像控制按钮?

标签 javascript jquery html css

如何在悬停时为每个图像制作图像控制按钮,如关闭、调整大小、旋转按钮。有一个 div,其中每个图像都是可拖动的。

我需要在每个图像附近制作一个图像控制按钮。只有当我们在图像上触摸或移动鼠标指针时,该按钮才需要可见,即按钮需要在其悬停效果中显示。

这是我的HTML

$( function() {
  var a = 1;
  $( ".child" ).draggable({
    containment: "parent",
    start: function(event, ui) { $(this).css("z-index", a++); }
  }).hover(function(){
    $(this).prepend('<button class="remove"></button>');
  }, function(){
    $(this).find('.remove').remove();
  }).on('click', '.remove', function(){
    $(this).closest('.child').remove();
  });
});
.parent{
  z-index:1;
  min-height: 200px;
  background-image: url('http://img.freepik.com/free-vector/white-canvas-background_1053-239.jpg?size=338&ext=jpg');
}
.child{
  position: absolute;
  z-index:1;
}

.remove{
  position: absolute;
  top: 0px;
  right: 0px;
  display:block;
  box-sizing:border-box;
  width:20px;
  height:20px;
  border-width:3px;
  border-style: solid;
  border-color:red;
  border-radius:100%;
  background: -webkit-linear-gradient(-45deg, transparent 0%, transparent 46%, white 46%,  white 56%,transparent 56%, transparent 100%), -webkit-linear-gradient(45deg, transparent 0%, transparent 46%, white 46%,  white 56%,transparent 56%, transparent 100%);
  background-color:red;
  box-shadow:0px 0px 5px 2px rgba(0,0,0,0.5);
  transition: all 0.3s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
<div class="parent">
  <div class='child'>
    <img src ='https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Circle_Red.png' />
  </div>
  <div class='child'>
    <img src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Wand-128.png' />
  </div>
  <div class='child'>
    <img src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Candy-01-128.png' />
  </div>
</div>

https://jsfiddle.net/felixtm/x3xb2jwf/7/

我需要这样的输出 enter image description here

我不需要十字线和边框框,三个 Angular 上只有 3 个按钮,一个用于关闭,一个用于调整大小,一个用于旋转

关闭按钮已经实现。对于轮换,我知道代码

var rotation = 0;
var rotating = false;
var startX = 0;

jQuery.fn.rotate = function(degrees) {
   $(this).css({'transform' : 'rotate('+ degrees +'deg)'});
};

$(document).mousemove(function(e){                
   if (!rotating) return;
   rotation = startX - e.clientX;
   $('.child').rotate(rotation);      
});

$(document).on("mouseup", function(){
   rotating = false;
});

 $('.rotateButton1').on("mousedown", function(e) {
   e.stopImmediatePropagation();
   rotating = true;
   startX = e.clientX;
});

请帮助使用这张图片实现这三个按钮和功能。谢谢。

最佳答案

要调整大小,请使用 .resizable() jQuery UI 的方法
对于旋转,使用 jquery-ui-rotatable插件。

.draggable().rotatable() 适用于 .child 元素和 .resizable()直接到图像。

<div class="parent">
  <div class='child'>
    <img class='child2' src ='https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Circle_Red.png' />
  </div>
  <div class='child'>
    <img class='child2' src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Wand-128.png' />
  </div>
  <div class='child'>
    <img class='child2' src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Candy-01-128.png' />
  </div>
</div>

jQuery:

var a = 1;

$( ".child" )
  .rotatable()
  .draggable({
    containment: "parent",
    start: function(event, ui) { $(this).css("z-index", a++); }
  })
  .hover(function(){
    $(this).find('.ui-rotatable-handle, .ui-resizable-handle, .remove').show();
  }, function(){
    $(this).find('.ui-rotatable-handle, .ui-resizable-handle, .remove').hide(); 
  })
  .append('<button class="remove"></button>')
  .on('click', '.remove', function(){
    $(this).closest('.child').remove();
  })
  .find( ".child2" ).resizable({
    // to keep aspect ratio:
    aspectRatio : true
  })
  // hide control handles:
  .trigger('mouseleave');

JSfiddle Demo

关于javascript - 如何使用css和js为div中的每个图像制作图像控制按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329075/

相关文章:

javascript - Angular Highcharts 从多对象元素读取

javascript - 如何使用Ctrl+f同时搜索下拉列表和网页内容?

javascript - 图像宽度在缓存时随着 onload 跟踪为零

html - valign ='middle' 工作,但没有正确显示文本仍然显示为 'bottom'

jquery - 为什么在调整页面大小时( Bootstrap )我的部分 html 代码消失了?

javascript - 如何使交替点击更改按钮内 div 的背景?

javascript - JS-正则表达式不工作

javascript - 类型 'replace' 上不存在属性 'IData' 。缺什么?

javascript - 如何在 JS 中使用动态名称调用函数

javascript - Safari 中的 HTML5 错误?使用本地文件时视频无法播放