javascript - Setting div position using coordinates - Stack Overflow

I'm having a web page (jsp) that included another jsp using<%@jsp:include ... %>When I click

I'm having a web page (jsp) that included another jsp using

     <%@jsp:include ... %>

When I click on the second part (included page), getting the co-ordinates using:

    window.event.clientX 
    window.event.clientY

and storing into x and y respectively. Now I'm setting a div to visible at that mouse co-ordinates like,

    document.getElementById('division').style.top=x;
    document.getElementById('division').style.left=y;

as expected to display that div at the same spot where the mouse clicked. But the division is ing somewhere else.

What is the reason for that?

I'm having a web page (jsp) that included another jsp using

     <%@jsp:include ... %>

When I click on the second part (included page), getting the co-ordinates using:

    window.event.clientX 
    window.event.clientY

and storing into x and y respectively. Now I'm setting a div to visible at that mouse co-ordinates like,

    document.getElementById('division').style.top=x;
    document.getElementById('division').style.left=y;

as expected to display that div at the same spot where the mouse clicked. But the division is ing somewhere else.

What is the reason for that?

Share Improve this question edited Feb 14, 2023 at 19:11 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 26, 2012 at 9:13 RAVITEJA SATYAVADARAVITEJA SATYAVADA 2,57123 gold badges57 silver badges92 bronze badges 3
  • Are you positioning your div as absolute or relative? – Priyank Patel Commented Oct 26, 2012 at 9:15
  • Does the div have any container div if not try to position it relative. – Priyank Patel Commented Oct 26, 2012 at 9:17
  • If relative, its position is not changing instead rendering in a constant position(actually where it is coded). – RAVITEJA SATYAVADA Commented Oct 26, 2012 at 9:22
Add a ment  | 

1 Answer 1

Reset to default 5

The top property is a string. Try document.getElementById('division').style.top=x + 'px';

I tried the following code and it worked fine for IE and Firefox. The onclick is for IE, the addEventListener is for pliant browsers.

<div id="clickMe" style="position: absolute; top: 10px;" onclick="myClick();">
    clickMe
</div>
<div id="division" style="position: absolute; top: 40px;">
    division
</div>
<script type="text/javascript">
    var clickMe = document.getElementById('clickMe');
    clickMe.addEventListener('click', myClick, 'false');

    function myClick(e) {
        var evt = e || window.event; //windows.event is for IE
        var x = evt.clientX;
        var y = evt.clientY;            
        document.getElementById('division').style.top = y + 'px';
        document.getElementById('division').style.left = x + 'px';
    }
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745321120a4622447.html

相关推荐

  • javascript - Setting div position using coordinates - Stack Overflow

    I'm having a web page (jsp) that included another jsp using<%@jsp:include ... %>When I click

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信