jquery - 在可拖动的 DIV 中添加图像按钮并在单击时切换图像源

标签 jquery html css position draggable

我有可拖动的 DIV。这些 DIV 中附加了一个图像。当鼠标悬停在 DIV 上时,图像的不透明度 更改为1。当鼠标离开时,图像不透明度变为 0

  1. 单击时我想将图像从 close_1.jpg 更改为图像 close_2.jpg - 并停止拖动该 DIV。

  2. 我想将图像的位置更改为左侧 + 顶部。 5 像素上部 DIV。

Example

CSS

div {
      width:200px;
      padding:10px;
      border: 2px solid #fff;
      cursor: pointer;
      background: #ccc;
}

HTML

<img class="close" src="http://ynternet.sk/test2/close_1.jpg" />
   <div>Demo 1 Div </div>
   <div>Demo 2 Div </div>
   <div>Demo 3 Div </div>

JavaScript

$(document).ready(function() {
  $('div').append( $("img.close").css("opacity", "0" ));
  $('div').find('img.close').css("paddingLeft", "20px");
  $('div').mouseover(function() {
      $(this).stop().animate({borderColor: "#aaa" }, "slow");
      $(this).find('img.close').css("opacity", "1");
  }).mouseout(function() {
      $(this).stop().animate({borderColor: "#fff" }, "slow");
      $(this).find('img.close').css("opacity", "0");
  })
  $('div').draggable({ grid: [ 10,10 ] });
});

最佳答案

添加这个将停止拖动并改变图像:

$('div img').click(function() {
    $(this).attr('src', 'http://ynternet.sk/test2/close_2.jpg').parent().draggable("destroy");
});​

jsFiddle example .

我只是不清楚您在 #2 中所说的“5px upper DIV”是什么意思。

关于jquery - 在可拖动的 DIV 中添加图像按钮并在单击时切换图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10092249/

相关文章:

css - 向事件链接添加样式?

javascript - 粒子地面.js : how to make website scrollable

javascript - 为元素设置多个EventListener,但删除单个EventListener

javascript - 如何在 ""javascript 中指定变量值

javascript - 上传图片时控制图片的宽度和高度

php - 使用 JQuery/AJAX 从 PHP 文件获取变量

javascript - ant进程中回显文件名

html - CSS 背景图像与选择器的高度/宽度不匹配?

jquery - 使用 jquery 将数组传递给 ColdFusion 组件

jquery - 如何在 jQuery 包装数组上使用 jQuery 选择器