简体   繁体   English

(CSS / jQuery)如何使用jQuery解决top和bottom属性之间的冲突

[英](CSS/jQuery) How resolve the conflict between top and bottom property with jQuery

Someone know how convert a bottom position to top with CSS transition and jQuery? 有人知道如何转换一个bottom位置, top与CSS过渡和jQuery?

I need to change the anchor of my image. 我需要更改图片的锚点。 And i have this problem. 而且我有这个问题。 There is a conflict between bottom and top . bottomtop之间存在冲突。

EDIT : In my scenario, the image has 100% of the width of the screen. 编辑 :在我的情况下,图像具有屏幕宽度的100%。 And when the window is resized, i have a code in JS whoes get the new position of the image. 当调整窗口大小时,我在JS中有一个代码来获取图像的新位置。 If my anchor is always "top" for example, in some situations I have this hole who show-up for severals milliseconds and if I set at this moment bottom instead of top it will fix my issue. 例如,如果我的锚点始终位于“顶部”,则在某些情况下,我会出现一个洞,该洞出现几毫秒,并且如果此刻将底部设置为顶部而不是顶部,则可以解决我的问题。 But I have this "jump" in the animation. 但是我在动画中有这个“跳跃”。

I made this fiddle to understand my issue. 我做了这个小提琴以了解我的问题。

Sorry for my English! 对不起我的英语不好! Someone have an idea about this? 有人对此有想法吗? Thank you ! 谢谢 !

You can get around the jumps by using a class, and removing the inline style as you go, like so: 您可以通过使用类来解决问题,并在运行时删除内联样式,如下所示:

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");
    });
}

and add a css class 并添加一个CSS类

.bottom {
    bottom: 0;
}    

as per http://jsfiddle.net/rZPq3/ 按照http://jsfiddle.net/rZPq3/

edit for cross-browser: 编辑跨浏览器:

    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/ http://jsfiddle.net/wCuuX/

You should stick with one of the properties to avoid conflicts. 您应该坚持使用其中一种属性以避免冲突。 I changed your fiddle to use top only and using a class to toggle the value instead. 我将您的小提琴更改为仅使用top并使用类来切换值。

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

See updated test case on jsFiddle . 请参阅jsFiddle上更新的测试用例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM