【问题标题】:Blind with negative margins负边距盲
【发布时间】:2014-01-14 15:34:15
【问题描述】:

我正在使用 JQuery UI,以便我可以使用盲功能向下滑动 div,但是,它无法正常工作。

这是我开始的 JSFiddle:http://jsfiddle.net/CBe3w/192/

由于某种原因,在滑动动画完成之前,侧面不会注册,此时它们会弹出。我怎样才能使边从头到尾注册(它们应该总是box类的宽度)?

HTML:

<div class="box">
    Click Me!
    <div class="footer">
        Why does it do this?
    </div>
</div>

CSS:

.box {
    background: #f5f5f5;
    width: 250px;
    padding: 25px;
}

.footer {
    background: red;
    padding: 15px 25px;
    margin-top: 25px;
    margin-left: -25px;
    margin-right: -25px;
    display: none;
}

JS:

$('.box').click(function() {
    $('.footer').toggle("blind");
});

【问题讨论】:

  • Bagwell,您能解释一下为什么选择 html 重新设计解决方案吗?很好奇,因为我的解决方案只暗示了 3 行 css 操作。

标签: javascript jquery jquery-ui


【解决方案1】:

解释: Jquery UI 盲效果将设置margin:0,同时它在你元素的高度上设置动画。

您将不得不重新设计您的 html 以拆分您的 .box 以删除它的填充,否则,修补 jquery-ui javascript 以从效果器中删除该 'margin:0',

您可以通过将内部容器定位为“相对”来解决此问题,因此无需重新制作 html。

.footer {
  background: none repeat scroll 0 0 #FF0000;
  display: none;
  left: -20px;
  margin-top: 25px;
  padding: 15px 25px;
  position: relative;
  right: 0;
  width: 100%;
}

jsFiddled here

【讨论】:

    【解决方案2】:

    我认为问题在于 jQuery 在切换元素时更改元素属性的顺序,以及您在页脚上设置了负边距这一事实。

    你可以去掉 .box 的左右内边距,然后把你的 .box 内容放在一个单独的 div 里面,里面有边距。不过,这是一种潜在的“hacky”方式。

    这是一个潜在的解决方案

    jQuery 保持不变,只是 CSS/HTML 发生了变化。

    jsfiddle

    HTML

    <div class="box">
        <div class="content">Click Me!</div>
        <div class="footer">
            The sides don't pop out anymore!
        </div>
    </div>
    

    CSS

    .box {
        background: #f5f5f5;
        width: 250px;
        /* took off the left and right padding */
        padding: 25px 0;
    }
    
    .content {
        /* "simulates" the padding of .box that you had before */
        margin: 0 25px;   
    }
    
    .footer {
        background: red;
        padding: 15px 25px;
        /* took off the negative margins */
        margin-top: 25px;
        display: none;
    }
    

    【讨论】:

    • Josh Beam,我缩进了你的 .css 部分。没什么大不了的(除了我的“压痕”精神疾病)。
    • @MilchePatern 这不是病 :) 在 HTML 和 CSS 代码之间添加 &lt;!
    【解决方案3】:

    你根本不需要 jQuery UI:LIVE DEMO

    $('.box').click(function() {
        $('.footer').slideToggle();
    });
    

    <div class="box">
        <h3>Click Me!</h3>
        <div class="footer">
            See how the sides popped Greatly?
        </div>
    </div>
    

    .box {
        background: #f5f5f5;
        width: 300px;
        padding-bottom: 15px;
    }
    .box h3{
        padding:25px 25px 10px;
    }
    .footer {
        background: red;
        padding: 15px 25px;
        display: none;
    }
    

    【讨论】:

    • 这看起来不错。不过,您甚至都不需要H3,对吗?除非只是为了它是语义的。如果他只是将 toggle("blind") 更改为 slideToggle(),OP 就不会出现侧面“弹出”和弹出的问题。
    • 我不想使用 slide() 因为它会“挤压”/扭曲内容。