douwen3836 2011-08-13 13:15
浏览 81
已采纳

如何使用带有COLORBOX插件和悬停事件的jQuery .live()函数

Currently I have the below jQuery which uses the colorbox plugin http://colorpowered.com/colorbox/

$(document).ready(function(){


  $(".altbgcolor").hover(function() {
        $(this)
            .addClass('altbgcolor-active')
            .find(".sharedp")
            .slideDown('slow');
    },function () {
        $(this)
            .removeClass('altbgcolor-active')
            .find(".sharedp")
            .slideUp('slow');
    });
 $(".example7").colorbox({
        onOpen:function(){ alert('onOpen: colorbox is about to open'); },
        onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content'); },
        onComplete:function(){ alert('onComplete: colorbox has displayed the loaded content'); },
        onCleanup:function(){ alert('onCleanup: colorbox has begun the close process'); },
        onClosed:function(){ alert('onClosed: colorbox has completely closed'); }
)};

 });

I want to convert both the hover functionality and the colorbox function to jQuery .live() so that any new elements added at runtime live, without refreshing the page, has the jQuery attached to them.

Can anyone help me with same? I tried for the hover event using below code, but it seems to record only the mouse enter event

$(".altbgcolor").live('hover',function() {
        $(this)
            .addClass('altbgcolor-active')
            .find(".sharedp")
            .slideDown('slow');
    },function () {
        $(this)
            .removeClass('altbgcolor-active')
            .find(".sharedp")
            .slideUp('slow');
    });

Please help!

EDIT:

Used the below solution, it works and though the .addClass('altbgcolor-active') seems to be working fine for the newly dynamically added elements, the .slideDown('slow') or .slideUp('slow') isnt working properly

 $(function() {
        $(".altbgcolor").live('mouseenter', function() {
              //hover code
              $(this)
                    .addClass('altbgcolor-active')
                    .find(".sharedp")
                    .slideDown('slow');
            }).live('mouseleave', function() {
              //stopped hovering code
              $(this)
                    .removeClass('altbgcolor-active')
                    .find(".sharedp")
                    .slideUp('slow');
            });
        });

Can someone please guide whats going wrong. You can see the demo of the problem at http://mashup2.sunnydsouza.com just scroll down the page and click 'more' button to fetch the new dynamic elements. They dont show the slideDown(), slideUp() effects :(

  • 写回答

1条回答 默认 最新

  • drbxr86044 2011-08-13 13:21
    关注

    hover is troublesome when used with live(). Instead, use mouseover and mouseout. From the jQuery docs:

    As of jQuery 1.4.3, you can bind multiple live event handlers simultaneously by passing a map of event type/handler pairs:

    $(".altbgcolor").live({
      mouseover: function() {
        $(this)
            .addClass('altbgcolor-active')
            .find(".sharedp")
            .slideDown('slow');
      },
      mouseout: function() {
        $(this)
            .removeClass('altbgcolor-active')
            .find(".sharedp")
            .slideUp('slow');
      }
    });
    

    EDIT: in response to the request for help with demo - check the HTML you are inserting as part of the "More" button's functionality. The new HTML does not contain the .sharedp div that your code is referencing, and appears to have been inserted a level too deep. Insert the correct HTML and the code will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算