Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

javascript - 如何在特定坐标中定位DIV?(How to position a DIV in a specific coordinates?)

I want to position a DIV in a specific coordinates ?(我想将DIV放在特定的坐标中?)

How can I do that using Javascript ?(我怎么能用Javascript做到这一点?)   ask by Adham translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Script its left and top properties as the number of pixels from the left edge and top edge respectively.(将其lefttop属性分别编写为左边缘和上边缘的像素数。)

It must have position: absolute;(它必须有position: absolute;)
var d = document.getElementById('yourDivId');
d.style.position = "absolute";
d.style.left = x_pos+'px';
d.style.top = y_pos+'px';

Or do it as a function so you can attach it to an event like onmousedown(或者将其作为一个函数执行,以便将其附加到onmousedown事件上)

function placeDiv(x_pos, y_pos) {
  var d = document.getElementById('yourDivId');
  d.style.position = "absolute";
  d.style.left = x_pos+'px';
  d.style.top = y_pos+'px';
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...