【问题标题】:(CSS/jQuery) How resolve the conflict between top and bottom property with jQuery(CSS/jQuery) jQuery如何解决top和bottom属性的冲突
【发布时间】:2013-07-05 04:33:02
【问题描述】:

有人知道如何使用 CSS 过渡和 jQuery 将 bottom 位置转换为 top

我需要更改图像的锚点。我有这个问题。 bottomtop 之间存在冲突。

EDIT :在我的场景中,图像具有 100% 的屏幕宽度。当窗口调整大小时,我在 JS 中有一个代码,可以获取图像的新位置。例如,如果我的锚始终是“顶部”,在某些情况下,我的这个洞会出现几毫秒,如果我此时设置底部而不是顶部,它将解决我的问题。但我在动画中有这个“跳跃”。

我发了this fiddle 来了解我的问题。

对不起我的英语!有人对此有想法吗?谢谢!

【问题讨论】:

  • 你想要达到的效果是什么?
  • 嗨@YotamOmer,我需要更改图像的锚点以防止出现一些错误的位置。

标签: javascript jquery css animation position


【解决方案1】:

您可以通过使用类来绕过跳转,并在执行过程中删除内联样式,如下所示:

if ( image.hasClass("bottom") ) {
    image.css("bottom", "").animate( { top: "0" } , 1000, function(){
        image.removeClass("bottom");
    });
} else {
    image.css("top", "").animate( { bottom: "0" } , 1000, function(){
        image.addClass("bottom");
    });
}

并添加一个 css 类

.bottom {
    bottom: 0;
}    

根据http://jsfiddle.net/rZPq3/

跨浏览器编辑:

    var top = image.position().top + 'px';
    var bottom = image.parent().height() - image.height() + 'px';

    if (image.hasClass("bottom")) {
        image.finish().css({ "bottom": "", "top": top }).animate({ top: "0px" }
            , 500, function () { image.removeClass("bottom"); });
    } else {
        image.finish().css({ "top": "","bottom": bottom }).animate({bottom:"0px"}
            , 500, function () { image.addClass("bottom"); });
    }

http://jsfiddle.net/wCuuX/

【讨论】:

  • 嗨@DomDay,感谢您的帮助。您的小提琴不适用于 Google Chrome。我在 Firefox 上测试了它的工作!如果你有谷歌浏览器的解决方案,这可能是我的解决方案!
  • 看起来 chrome 不会动画,除非在动画之前明确设置了起始顶部或底部属性。所以试试这个:jsfiddle.net/wCuuX
【解决方案2】:

您应该坚持使用其中一种属性以避免冲突。我将您的小提琴更改为仅使用 top 并使用类来切换值。

var toggle = $("button#toggle");
var image = $("div img");
toggle.click(function () {
    image.toggleClass("toggled");
});

updated test case on jsFiddle

【讨论】:

  • 您好@MarcusEkwall,感谢您的回答。不幸的是,我需要更改我的锚以防止我的图像位置错误(对不起,由于我的英语不好,我很难解释......)。在我的场景中,图像具有 100% 的屏幕宽度。当窗口调整大小时,我在 JS 中有一个代码,可以获取图像的新位置。例如,如果我的锚总是“顶部”,在某些情况下,我的这个洞会出现几毫秒,如果我此时设置bottom 而不是top,它将解决我的问题。但我在动画中有这个“跳跃”。
  • 为什么不使用bottom呢?
  • @Fresheyeball 嗨!如果我使用bottom 我的问题是一样的,只是倒置(在某些情况下,我在图像顶部有一个洞)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 2014-10-19
相关资源
最近更新 更多