【问题标题】:How to make image control button for each image inside the div using css and js?如何使用css和js为div内的每个图像制作图像控制按钮?
【发布时间】:2017-03-12 18:18:10
【问题描述】:

如何在悬停时为每个图像制作图像控制按钮,如关闭、调整大小、旋转按钮。有一个 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/

我需要这样的输出

我不需要交叉线和边框框,三个角只有 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;
});

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

【问题讨论】:

  • startXendX 从鼠标坐标被用作rotation?那是错的!三角菲利克斯!
  • 你能帮忙解决这个问题吗?解决这整个问题?谢谢。
  • @RokoC.Buljan 怎么了?请帮忙解决这个问题
  • 抱歉,您的.rotateButton1 在哪里?您应该创建一个反映您的问题的minimal reproducible example
  • 这可能会对您有所帮助。你必须自己微调它;-) jsfiddle.net/x3xb2jwf/9

标签: javascript jquery html css


【解决方案1】:

要调整大小,请使用 jQuery UI 的 .resizable() 方法
对于旋转,请使用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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 2021-03-01
相关资源
最近更新 更多