Javascript 如何在javascript中设置偏移量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951195/
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-22 22:02:31  来源:igfitidea点击:

how to set the offset in javascript

javascriptjquerycss

提问by Santhosh

How can set the offset using JavaScript ?

如何使用 JavaScript 设置偏移量?

回答by Sampson

You set the leftand topCSS values.

您设置lefttopCSS 值。

With jQuery

使用 jQuery

$("#myEl").css({"position":"absolute","left":"100px","top":"100px"});

Vanilla JavaScript

原生 JavaScript

var myEl = document.getElementById("myEl);
myEl.style.position = "absolute";
myEl.style.left = "100px";
myEl.style.top = "100px";

回答by Aaron Newton

You could also add a class, e.g.

您还可以添加一个类,例如

Jquery: $("p:last").addClass("selected");

Jquery: $("p:last").addClass("selected");

Javascript: Someone has posted a JS function here - http://bytes.com/topic/javascript/answers/542128-can-javascript-add-new-style-class

Javascript:有人在这里发布了一个 JS 函数 - http://bytes.com/topic/javascript/answers/542128-can-javascript-add-new-style-class

You would then need to make sure that 'selected' was defined in your style sheet. The relevant CSS properties are:

然后,您需要确保在样式表中定义了“selected”。相关的 CSS 属性是:

top left margin padding

左上边距填充

You probably want a combination of margin (offset outside element) and padding (offset inside element).

您可能需要边距(元素外偏移)和填充(元素内偏移)的组合。