简体   繁体   English

使用jquery在悬停时添加div

[英]Add div on hover using jquery

I build a project something like "trip planner" but I need to add on my cloned div-object an verticl line on left border with css: 我建立了一个类似“旅行计划者”的项目,但我需要在我的克隆div对象上添加一个带有css的左边界的垂直线:

.line
  { width:5px, height:200px; background:red;}

so to be something like this (you can see on hover an vertical line) 所以是这样的 (你可以看到悬停在垂直线上)

I was try to do this with code: 我试着用代码做这个:

$(function() {
    //$( ".draggable" ).resizable();
    $( ".draggable" ).draggable({
      revert: 'invalid', 
      helper:"clone",
      snap: "#drop_here td", 
      opacity: 0.7
    });
    $( "#drop_here td" ).droppable({
      // accept only from left div, 
      // this is necessary  to prevent clones duplicating inside droppable
      accept: '#left .draggable',
      drop: function( event, ui ) {
        // 4 append clone to droppable
        $( this ).append(
          // 1 clone draggable helper
          $(ui.helper).clone()

          // 2 make the clone draggable
          .draggable({
             containment:"#table",
            snap: "#drop_here td" 
          })
          // 3 make the clone resizable
          .resizable());

//HERE IS MY PROBLEM IN CODE
        $(".draggable").hover(function() {
    $(this).append("<div class='line'></div>");
}, function() {
    $(this).removeClass("line");
});
      }
    });
  });

but dont work! 但是不行!

DEMO DEMO

The first problem is that you're css has a , insted of a ; 第一个问题是,你的CSS有, insted的的;

.line { 
 display:none;
 width: 5px; 
 height:200px;
 background: red;

} }

then for the jquery modify like this: 然后为jquery修改如下:

$('.draggable').hover(function(){
    $(this).find('.line').show();
}, function() {
    $(this).find('.line').hide();
});

In your markup place a .line (only one ) just after every .draggable , but not on hover or it will append it every time you hover the .draggable creating tons of .line s 在你的标记中, after每个.draggable after放一个.line (只有一个 ),但不是在hover或者每次你hover .draggable创建大量的.line s时它都会append

jsfiddle : http://jsfiddle.net/steo/JB7jN/1/ jsfiddle: http//jsfiddle.net/steo/JB7jN/1/

Your "hover out" handler removes the class from $(this) -- not from the appended child. 你的“悬停”处理程序从$(this)中删除了类 - 而不是从附加的子项中删除。

It's probably bad practice to dynamically create elements on hover, that are never deleted & will presumably gradually fill the document with garbage. 在悬停时动态创建元素可能是不好的做法,这些元素永远不会删除,并且可能会逐渐用垃圾填充文档。

You have to bind the .hover() in the document ready like this: 您必须将文件中的.hover()绑定到这样:

$(document).ready(function(){
      $(".draggable").hover(function() {
          $(this).append("<div class='line'></div>");
      }, function() {
          $(this).children('.line').remove();
      });
});

if you only want to add the line to the clone add the start property to the draggable options like this: 如果您只想将行添加到克隆,请将start属性添加到可拖动选项,如下所示:

$( ".draggable" ).draggable({
  revert: 'invalid', 
  helper:"clone",
  snap: "#drop_here td", 
  opacity: 0.7,
  start: function(event, ui){
    var clone = $(ui.helper);
    if (clone.children('div.line').length == 0)
        clone.append("<div class='line'></div>");
  }
});

Tested this and it works like a charm.. Dont forget to remove this part: 测试了它,它就像一个魅力..别忘了删除这部分:

    $(".draggable").hover(function() {
         $(this).append("<div class='line'></div>");
    }, function() {
         $(this).removeClass("line");
    });

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

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