【问题标题】:jQueryUI show/hide animation issue with child nodesjQueryUI 显示/隐藏子节点的动画问题
【发布时间】:2012-02-29 20:19:12
【问题描述】:

我在使用 jQueryUI 的 show()/hide() 函数时遇到问题。他们基本上在动画期间停用了我的 CSS。

jsFiddle 链接:http://jsfiddle.net/UBATE/1/

根据 API:

http://docs.jquery.com/UI/Effects/Drop,

我已经构建了以下函数:

updateTeamLists = function(args) {
    // fade out -> rebuild the content -> fade in

    $("#team1, #team2").hide("drop", {"direction": "right"}, 600, function() {
        rebuildTeamLists(args); 
        $(this).show("drop", {"direction": "right"}, 600);  
    });     
};

子 DOM 节点在动画开始时似乎失去了所有 CSS 属性,即它们停止浮动(或失去其内联块显示)并且它们的背景立即消失。但是,动画和文本的淡出效果很好。

HTML DOM 结构:

<div id="teams">
   <div id="team1">
      <ul>
         <li>Player 1</li>
         <li>Player 2</li>
         <li>Player 3</li>
         <li>Player 4</li>
         <li>0 Pts</li>
      </ul>
   </div>
   <div id="team2">
      <ul>
         <li>Player 5</li>
         <li>Player 6</li>
         <li>Player 7</li>
         <li>Player 8</li>
         <li>0 Pts</li>
      </ul>
   </div>
</div>

CSS:

#content > #teams {
    margin-top: 10px;
    padding: 5px 5px 5px 22px;
    width: 650px;
    border: 2px solid #bfbfbf;

    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;

    box-sizing: border-box; 
    -moz-box-sizing:border-box;
}

#content > #teams > div {
    margin-top: 5px;
    display: block;
    height: 28px;
}

#content > #teams > div:first-child {
    opacity: 1.0;   
}

#content > #teams > div:last-child {
    opacity: 0.6;
}

#content > #teams > div > ul > li {
    height: 25px;
    width: 125px;
    border-width: 1px;
    border-style: solid;
    margin-left: 5px;   
    display: inline-block;
}

#content > #teams > div > ul > li:first-child {
    -webkit-border-top-left-radius: 20px;
    -webkit-border-bottom-left-radius: 20px;
    -moz-border-radius-topleft: 20px;
    -moz-border-radius-bottomleft: 20px;
    border-top-left-radius: 20px;
    border-bottom-left-radius: 20px;
}

#content > #teams > div > ul > li:last-child {
    -webkit-border-top-right-radius: 20px;
    -webkit-border-bottom-right-radius: 20px;
    -moz-border-radius-topright: 20px;
    -moz-border-radius-bottomright: 20px;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;

    width: 50px !important;
}

#content > #teams > #team1 > ul > li {
    background: rgb(229,130,130); /* Old browsers */
    background: -moz-linear-gradient(top, rgb(229,130,130) 0%, rgb(216,43,43) 51%, rgb(232,153,153) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgb(229,130,130)), color-stop(51%,rgb(216,43,43)), color-stop(100%,rgb(232,153,153))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgb(229,130,130) 0%,rgb(216,43,43) 51%,rgb(232,153,153) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgb(229,130,130) 0%,rgb(216,43,43) 51%,rgb(232,153,153) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgb(229,130,130) 0%,rgb(216,43,43) 51%,rgb(232,153,153) 100%); /* IE10+ */
    background: linear-gradient(top, rgb(229,130,130) 0%,rgb(216,43,43) 51%,rgb(232,153,153) 100%); /* W3C */
    border-color: rgb(229,130,130);

    text-align: center;
    padding-top:3px;
}

#content > #teams > #team2 > ul > li {
    background: #b3dced; /* Old browsers */
    background: -moz-linear-gradient(top, #b3dced 0%, #29b8e5 50%, #bce0ee 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#b3dced), color-stop(50%,#29b8e5), color-stop(100%,#bce0ee)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #b3dced 0%,#29b8e5 50%,#bce0ee 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #b3dced 0%,#29b8e5 50%,#bce0ee 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #b3dced 0%,#29b8e5 50%,#bce0ee 100%); /* IE10+ */
    background: linear-gradient(top, #b3dced 0%,#29b8e5 50%,#bce0ee 100%); /* W3C */
    border-color: rgb(179,220,237);

    text-align: center;
    padding-top:3px;
}

很遗憾,我无法在线托管演示代码,所以我发布了我得到的所有内容。我正在使用 Google Chrome 17.0.963.56 和 Firefox 10.0.2。

我经常在将 jQueryUI 效果添加到我的脚本时遇到问题,最终没有添加它们。虽然可能有一些技巧/错误阻止它工作(例如,如果您的 DOM 节点没有类属性,toggleClass 动画将不起作用)。

我错过了什么吗?解决方法如何,我是否必须遍历所有子节点?

提前致谢!

哈蒂

【问题讨论】:

标签: css jquery-ui show-hide


【解决方案1】:

Jquery UI 将在受影响的元素下添加一个额外的 div。示例:

<div id="team1">
<ul>
  ...
<ul>
</div>

应用效果时,结构为:

<div id="team1">
<div class="jquery-ui something-else">
<ul>
  ...
</ul>
</div>
</div>

解决方案是不使用直接子选择器“>”。请参阅示例here。您可以将其与您的版本进行比较:)

【讨论】:

  • 谢谢,这暂时会有所帮助。您的解决方案存在一点 y 偏移问题,但可以通过编辑“#content > #teams div”行来解决。但是,当我将其更改为“#content > #teams #team1, #content > #teams #team2”时,margin-top 将消失。我对 CSS 并不是很陌生,但仍然很困惑......而且我担心我以某种方式覆盖了你有用的小提琴。你现在可以在下面找到它:jsfiddle.net/UBATE/12 这就是我要说的:jsfiddle.net/3NHSR
  • 我认为是因为你错误地添加了一个逗号:) #teams #team1, #teams #team2, {
  • Firebug 或 chrome 的开发工具在编写 css 时非常有用
  • 错字不是问题,但我通过将 margin-top 替换为 margin-bottom 来解决问题,这样 jqueryUI div 就不会影响设计。
  • 实际上不是在受影响的元素周围插入了额外的div吗?我的测试表明这一点..
猜你喜欢
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 2016-02-02
相关资源
最近更新 更多