【问题标题】:jQuery Animation Issue On MouseenterMouseenter 上的 jQuery 动画问题
【发布时间】:2011-11-16 03:30:06
【问题描述】:

所以我在编写网站的这个小部分时偶然发现了自己的方式,但它有点错误。我知道 jquery 足够危险,但我希望有人可以帮助我使它变得更好、更高效。

以下是我认为可以改进的方法,但我不知道如何完成这些:

  1. 翻过第一个城市后,移动到下一个城市时,它 不应该带回所有其他标记然后重新隐藏它们。它 应该只是隐藏你所在的标记,然后显示你所在的标记 目前已经结束,但是鼠标悬停和延迟会带回所有标记。
  2. 目前,如果您从一个翻转到另一个翻转 节奏快,动画好像假发了
  3. 这是一个奖励,但我最初设想标记会 以递增的顺序制作动画,而不是同时制作动画。 可以是随机的,也可以是定义的。我认为这会使 动画看起来更流畅。我认为这需要一个循环 但就像我之前提到的,我只知道危险。

这里是页面链接:http://204.12.117.109/~sandbox/map/

这是我正在使用的 jquery 代码:

$(function() {
        $('.city').mouseenter(function(){

            var relid = $(this).attr('rel');

            $("#communities div[id!=" + relid + "].pin").animate({
                opacity: 0,
                top: '-=40',
                }, 500);
        });
        $('.city').mouseleave(function(){

            var relid = $(this).attr('rel');

            $("#communities div[id!=" + relid + "].pin").animate({
                opacity: 1,
                top: '+=40',
                }, 500, 'easeOutBounce');
        });
    });

这是我正在使用的 CSS:

#communities { background: url(images/bgCommunities.gif) scroll bottom right no-repeat; position: relative; }
    #communities .lists { width: 340px !important; }
    #cities li { width: 160px !important; margin: 0px 2px !important; padding: 4px 0 4px 4px; }
    #cities li:hover { background-color: rgb(240,240,240); }
        #boyle, #long, #santa, #rich, #fres, #sac, #city, #sLA, #coach, #kern, #oak, #merc, #sal, #del { position: absolute; width: 8px; height: 12px; background: url(images/pin.png) 0 0 no-repeat; }
        #boyle { top: 230px; right: 95px; }
        #long { top: 241px; right: 92px; }
        #santa { top: 236px; right: 84px; }
        #rich { top: 139px; right: 165px; }
        #fres { top: 173px; right: 121px; }
        #sac { top: 133px; right: 147px; }
        #city { top: 258px; right: 78px; }
        #sLA { top: 235px; right: 90px; }
        #coach { top: 239px; right: 62px; }
        #kern { top: 206px; right: 105px; }
        #oak { top: 148px; right: 162px; }
        #merc { top: 161px; right: 136px; }
        #sal { top: 171px; right: 155px; }
        #del { top: 71px; right: 185px; }

这是我正在使用的 HTML:

<div id="communities">
<div class="lists">
    <ul id="cities">
        <li class="city" rel="boyle"><a href="#">Boyle Heights</a></li>
        <li class="city" rel="long"><a href="#">Long Beach</a></li>
        <li class="city" rel="santa"><a href="#">Central Santa Ana</a></li>
        <li class="city" rel="rich"><a href="#">Richmond</a></li>
        <li class="city" rel="fres"><a href="#">Central/SE/SW Fresno</a></li>
        <li class="city" rel="sac"><a href="#">Sacramento</a></li>
        <li class="city" rel="city"><a href="#">City Heights</a></li>
        <li class="city" rel="sLA"><a href="#">South Los Angeles</a></li>
        <li class="city" rel="coach"><a href="#" >Coachella Valley</a></li>
        <li class="city" rel="kern"><a href="#">South Kern</a></li>
        <li class="city" rel="oak"><a href="#">East Oakland</a></li>
        <li class="city" rel="merc"><a href="#">SW Merced/East Merced</a></li>
        <li class="city" rel="sal"><a href="#">East Salinas (Alisal)</a></li>
        <li class="city" rel="del"><a href="#">Del Norte County</a></li>
    </ul>
</div>
<div class="pin" id="boyle"></div>
<div class="pin" id="long"></div>
<div class="pin" id="santa"></div>
<div class="pin" id="rich"></div>
<div class="pin" id="fres"></div>
<div class="pin" id="sac"></div>
<div class="pin" id="city"></div>
<div class="pin" id="sLA"></div>
<div class="pin" id="coach"></div>
<div class="pin" id="kern"></div>
<div class="pin" id="oak"></div>
<div class="pin" id="merc"></div>
<div class="pin" id="sal"></div>
<div class="pin" id="del"></div>

【问题讨论】:

  • 请重命名您的标题“如何修复错误代码?”以便其他人更容易理解您的问题。你的标题现在太笼统了。还可以尝试让您的 CSS 和 HTML 更简单,以便更容易通过。
  • 嘿 Tuva,很抱歉,我真的不知道如何在不破坏功能的情况下使 CSS 和 HTML 更简单。我不知道您是否看到了我的链接,但如果您还没有看到正在运行的代码,可能会解决问题:link

标签: jquery animation rel


【解决方案1】:

知道了(02:31)! (现在正在开发随机版本……我喜欢挑战!):

编辑(见http://zequinha-bsb.int-domains.com/map/cities.html

知道了 2 @ 3:29am EST(我是按小时计酬的……还是根本没有?) 随机版本位于底部及其各自的链接。

<script type="text/javascript">

    var firstEntry = true;
    var lastOn = '';

    function showAllPins() {
        if ($('#communities').hasClass('theMouseIsOff')) {
            $("#communities div[id!=''].pin").animate({
                opacity: 1,
                top: '+=40',
            }, 500, 'easeOutBounce');
            firstEntry = true;
            $('#communities').removeClass('theMouseIsOff');
        }
    }
    function showPin(relid){
        lastOn = relid;
        if ($('#communities').hasClass('theMouseIsOff')) $('#communities').removeClass('theMouseIsOff');
        if (firstEntry == true) {
            $("#communities div[id!=" + relid + "].pin").animate({
                opacity: 0,
                top: '-=40',
            }, 500);
            firstEntry = false;
        } else {
            $("#communities div[id=" + relid + "].pin").animate({
                opacity: 1,
                top: '+=40',
            }, 500, 'easeOutBounce');
        }
    }
    function removeLastPin() {
        $('#communities').addClass('theMouseIsOff');
        $("#communities div[id=" + lastOn + "].pin").animate({
            opacity: 0,
            top: '-=40',
        }, 500);
        setTimeout('showAllPins()',5000);
    }

    $(document).ready( function () {
        $('.city').mouseenter( function () {
            relid = $(this).attr('rel');
            showPin(relid);
        }).mouseleave( function () { removeLastPin() });
    });

</script>

随机版本:

<script type="text/javascript">

    var firstEntry = true;
    var lastOn = '';

    function showAllPins() {
        if ($('#communities').hasClass('theMouseIsOff')) {
            var citiesArr = [];
            $('.pin').each( function () { 
                citiesArr.push(this.id);
                $('#'+this.id).hide();
            });
            var stillHidden = citiesArr.length;
            while (stillHidden > 0) {
                var a = Math.floor(Math.random()*citiesArr.length);
                if ($('#'+citiesArr[a]).is(':hidden')) {
                    $('#'+citiesArr[a]).show().delay(Math.floor(Math.random()*900)).animate({
                        opacity: 1,
                        top: '+=40',
                    }, Math.floor(Math.random()*900), 'easeOutBounce');
                    stillHidden--;
                }
            }
            firstEntry = true;
            $('#communities').removeClass('theMouseIsOff');
        }
    }
    function showPin(relid){
        lastOn = relid;
        if ($('#communities').hasClass('theMouseIsOff')) $('#communities').removeClass('theMouseIsOff');
        if (firstEntry == true) {
            $("#communities div[id!=" + relid + "].pin").animate({
                opacity: 0,
                top: '-=40',
            }, 500);
            firstEntry = false;
        } else {
            $("#communities div[id=" + relid + "].pin").animate({
                opacity: 1,
                top: '+=40',
            }, 500, 'easeOutBounce');
        }
    }
    function removeLastPin() {
        $('#communities').addClass('theMouseIsOff');
        $("#communities div[id=" + lastOn + "].pin").animate({
            opacity: 0,
            top: '-=40',
        }, 500);
        setTimeout('showAllPins()',2000);
    }

    $(document).ready( function () {
        $('.city').mouseenter( function () {
            relid = $(this).attr('rel');
            showPin(relid);
        }).mouseleave( function () { removeLastPin() });
    });

</script>

EDIT(在removeLastPin中删除了relid作为参数)

您可以在这里查看:http://zequinha-bsb.int-domains.com/map/randomCities.html

【讨论】:

  • 感谢 zeq 的回复,但看起来这只适用于第一次翻转。之后,它只是不断将标记向下推到页面下方。
  • 我正在尝试复制您的网站,以便制作最终版本。我无法下载精灵,因为 ../images/... 无法从沙箱中访问。我确实发现了一个错误:show all 上的 "" 应该是 id=='';不是 id=="" (单引号而不是双引号)。
  • 这里我已将您的代码加载到 jsfiddle 中:jsfiddle.net/luniablue/e4LWU/15
  • 看起来棒极了。非常感谢您帮助我解决这个问题!
  • 请对答案进行投票并将其选为您接受的答案。先感谢您。顺便说一句:伟大的项目!
【解决方案2】:

这个例子应该会让你明白。

http://jsfiddle.net/wJnr5/3

您需要做的是将事件绑定到包含城市的容器。当鼠标离开这个容器时,就是你想要重新显示所有城市的时候。我还使用了一个更好的 jQuery 选择器来获取除当前城市之外的所有内容。

$('.city').bind('mouseenter', 
    function() {
        //fade out everyone but me
        $('.city').not($(this)).each( function() {                            
            $(this).stop().delay(Math.floor(Math.random()*1000)).animate({ opacity: 0 });
        });
        //fade me in
        $(this).stop().animate({ opacity: 1 });
    }
);

$('#container').bind('mouseleave', function() {
    $('.city').stop().animate({ opacity: 1 });    
});

【讨论】:

  • 现在更好了。城市根据规范 3 随机淡入。
  • 感谢您的回复。我会确保将其存档以备将来使用,但不幸的是,这不是我制作动画的文本。文本链接是针对绝对定位在地图上的标记图标。当针对绑定元素之外的多个元素时,绑定解决方案可以工作吗?查看我的功能链接:[link]204.12.117.109/~sandbox/map
  • 代码最终将以完全相同的方式工作。只需更改选择器表达式以定位您的标记而不是您的城市。例如,将.city 更改为.marker。您将无法使用 $(this),但只需像在旧代码中一样选择您想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
相关资源
最近更新 更多