jQuery 如何使用jquery从左到右更改css位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19182584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:23:52  来源:igfitidea点击:

How to change css position left to right using jquery?

jquerycss

提问by sun

I have a div with leftvalue i wanted to change it to rightside with values

我有一个带有left值的 div,我想将其更改为right带有值的

#check
{
    left:50px;
    top:20px;
    position:relative;
    width:100px;
    height:100px;
    background:rgb(100,200,0);
}

So i tried jquery like this

所以我试过这样的jquery

$(document).ready(function(){

    $('#check').css('right','20px');

});

is this possible?.i tried searching but all are with the same attribute different value changes. Here is the Fiddle

这可能吗?。我尝试搜索,但都具有相同的属性不同的值更改。 这是小提琴

回答by Mark

You have to set leftto default, and the value for right:

您必须设置left为默认值,以及以下值right

$(document).ready(function(){

 $('#check').css({
  'right':'20px',
  'left': 'auto'
 });

});

JSfiddlethanks to MackieeE

JSfiddle感谢MackieeE

回答by David Jones

You will need to remove the left then set the right.

您将需要删除左侧然后设置右侧。

For example:

例如:

$('#check').css('right', '20px').css('left', '');

回答by Dharmesh Thanki

Try below code.

试试下面的代码。

$(document).ready(function(){    
    $('#check').css({'right':'20px'});    
});

回答by foxdanni

simply as it :

简单地说:

 $(document).ready(function(){

    $('#check').css('left','auto').css('right','20px');

 });

example : http://jsfiddle.net/mwX8Z/5/

示例:http: //jsfiddle.net/mwX8Z/5/

回答by Arun P Johny

Try

尝试

#check {
    left: 50px;
    top:20px;
    position:absolute;
    width:100px;
    height:100px;
    background:rgb(100, 200, 0);
}

and

$(document).ready(function () {
    $('#check').css({
        'left': 'auto',
        'right': '20px'
    });

});

Demo: Fiddle

演示:小提琴

回答by Ramy Alayane

found something very interesting. To animate something to the right do something like:

发现了一件很有趣的事情。要使某些内容向右移动,请执行以下操作:

$(".something").animate({
 left:"",
 right:"",
 marginRight:"10px"
},300);

This is like setting it to right: 10px;

这就像将其设置为正确的:10px;