Santhosh
Santhosh

Reputation: 20406

how to set the offset in javascript

How can set the offset using JavaScript ?

Upvotes: 16

Views: 47068

Answers (3)

zheng
zheng

Reputation: 478

If the container is scrollable, the scroll method may be more in line with the specification. If not, it has to be solved by setting the absolute positioning

Element.scroll() - Web APIs | MDN

// Put the 1000th vertical pixel at the top of the element
element.scroll(0, 1000);

// use optionas
element.scroll({
  top: 100,
  left: 100,
  behavior: 'smooth'
});

Upvotes: 2

Sampson
Sampson

Reputation: 268324

You set the left and top CSS values.

With jQuery

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

Vanilla JavaScript

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

Upvotes: 25

Aaron Newton
Aaron Newton

Reputation: 2306

You could also add a class, e.g.

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

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

top left margin padding

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

Upvotes: 0

Related Questions