【问题标题】:write mouseout state within array of dynamic if else hover states在动态数组中写入 mouseout 状态 if else 悬停状态
【发布时间】:2015-07-31 16:55:03
【问题描述】:

我在一个数组中调用了一系列悬停状态,其中每个唯一项目显示唯一的悬停内容(在同一个悬停类中。样式相同,但内容不同。)

更新:以下是完整的 JS:

$('.meJane, .antarcticAntics, .pigeon, .marchOn, .nelson, .president, .owen, .tealBook, .children, .dot, .overlayContent').hover(function(){
    var location = $(this).offset();
    console.log('location: ', location);
    $('.overlayContent').css({'display': 'inline-block', 'height': ($(this).height()+'px'), 'width': ($(this).width()+'px'), 'top': (location.top - $('#schlMainContent').offset().top), 'left': (location.left - $('.classicBooks').offset().left)});
    $('.overlayContent', this).show();
    var bookName = $(this).attr('class');
    if (bookName == 'meJane') {
        // links for jane
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        }); 
   } else if (bookName == 'antarcticAntics') {
        // links for antarcticAntics
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'pigeon') {
        // links for pigeon
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'marchOn') {
        // links for marchOn
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'nelson') {
        //links for nelson
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'president') {
        // links for president
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'owen') {
        // links for owen
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'tealBook') {
        // links for tealBook
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'children') {
        // links for children
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } else if (bookName == 'dot') {
        // links for dot
        $('#previewLink').attr('class', 'play' + bookName);
        $('#shopLink').attr('href', 'http://www.uniquelink.com');
        $('.preview').click(function(){ // .preview is same as openPopup
            console.log('bookName: ', bookName);
            openPopUp(bookName);
        });
    } 

我需要添加一个 mouseOut 状态,因为目前,删除悬停状态的唯一方法是当您悬停到下一个项目时。当您将鼠标移出相关区域时,我需要将其完全删除。任何想法如何通过mouseout().

合并返回正常状态

尝试,鼠标离开

$('.meJane, .antarcticAntics, .pigeon, .marchOn, .nelson, .president, .owen, .tealBook, .children, .dot, .overlayContent').on('mouseleave',function(){
    $('.overlayContent').css({'display': 'none'});

尝试:

$( ".meJane, .antarcticAntics, .pigeon, .marchOn, .nelson, .president, .owen, .tealBook, .children, .dot, .overlayContent" ).off( "mouseleave" );
});

另外,尝试在末尾添加另一个其他内容

    } else {
    $( ".meJane, .antarcticAntics, .pigeon, .marchOn, .nelson, .president, .owen, .tealBook, .children, .dot, .overlayContent" ).css({'display': 'none'});
    }
});

【问题讨论】:

  • 刚刚试了一下。更新了完整的问题 JS。
  • 如果您看到超过 5 个 else if 使用相同的代码,请尝试其他方法/重构

标签: javascript jquery mouseleave mouseout


【解决方案1】:

.hover 函数实际上接受第二个参数,该参数将在鼠标移出时调用。看看下面的代码:

$('.selectors').hover(function() {
    // mouse in
    $('.something').show();
}, function() {
    // mouse out
    $('.something').hide();
});

查看手册了解更多详情:http://api.jquery.com/hover

还有一个非常基本的示例来演示:http://jsfiddle.net/j36mw6zr/

【讨论】:

  • 非常感谢,这是有道理的,我尝试将鼠标中的所有悬停 if 语句包装进去并用隐藏创建新鼠标,但它都被忽略了。也许因为我有 if 语句的动态悬停,所以它与简单的 show hide 有点不同。
  • 我认为问题不在于如何在这样的简单场景中做到这一点,而是在我的场景中满足条件。
  • 回调函数中的内容真的不重要。试着用你目前所拥有的东西来设置一个工作小提琴,我很乐意修改和演示
猜你喜欢
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2012-04-26
相关资源
最近更新 更多