简体   繁体   English

jQuery .css(“ margin-left”)

[英]jQuery .css(“margin-left”)

I made a kind of game in javascript and in this game there are some divs which are horizontally moving with different transition times. 我用JavaScript开发了一种游戏,在这个游戏中,有一些div在不同的过渡时间水平移动。 In this game there is a function which is called each 1/100 seconds. 在此游戏中,有一个函数每1/100秒被调用一次。 Then it first checks it position with: 然后,它首先使用以下命令检查其位置:

$("class1").css("margin-left") 

And now the weird thing: when I set the class in html like: 现在奇怪的是:当我在html中设置类时,如下所示:

<div class = "class1"></div> 

The .css("margin-left") works perfectly, but when I don't set a class in html and I add the class in js like: .css("margin-left")可以完美地工作,但是当我没有在html中设置类并且在js中添加该类时,如下所示:

$("div:nth-child(6)").addClass("class1");

Then the $(theclass).css("margin-left") give wrong outputs. 然后$(theclass).css("margin-left")输出错误。 I tried with the .position and .offset but those both didn't work for me. 我尝试使用.position和.offset,但是它们都不适合我。

My code: 我的代码:

function newBlock(block, marginTop, bgc, height, hoogte1, hoogte2, v, tijd){
                        block.css("background-color", bgc).css("height", height).css("margin-top", marginTop);
                        var movement1 = "movement1";
                        var movement2 = "movement2";
//                        if (positionPixelsBlockF > 0){
//                            positionPixelsBlockF = positionBlockF.slice(0, 3);
//                        }
                        setInterval(positie, 10);
                        function positie(){
                        tijd = tijd + 1;
                        if (v != 0){
                            block.addClass("i" + v);
                            $(".i" + v).css({"width": "50px", "display": "inline-block", "position": "absolute", "z-index": "20"});
                            var positionBlockF = $(".i" + v).css("margin-left");
                            var positionPixelsBlockF = positionBlockF.slice(0, 4);
                            $("#uitleg4").html(positionPixelsBlockF);
else if (v === 0){
                            var positionBlockF = $(".b1").css("margin-left");
                            var positionPixelsBlockF = positionBlockF.slice(0, 4);

Don't mind the incomplete use of the {}, but in my code that's fixed. 不要介意{}的不完整使用,但是在我的代码中已修复。

PS: using brackets PS:使用括号

I believe you are missing a dot before class1 selection, in jquery there are two types of elements selection either by class like $(".ElemnetClass") OR by ID like $("#ElementID") 我相信您在选择class1之前缺少dot ,在jquery中有两种类型的元素选择,要么是按类的$(".ElemnetClass")要么是按ID的类型像$("#ElementID")

More info about .css() 有关.css()的更多信息

This Is An Example Of Your Requirement 这是您需求的一个例子

 $(".class").css('margin-left','10px');// Some Px (or) percentage Your Based On Requirement

You Can Use This Type Also Firstly You Create One Class Of Your Requirements And Use Below Code 您也可以使用此类型,首先创建一类需求并使用以下代码

$("#ElementId").addClass('created classname Here');

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

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