最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set the cursor position on the screen? - Stack Overflow

programmeradmin4浏览0评论

Is there any way I can set the cursor position off the screen/webpage or on top/bottom of the page when a page is load? If so how can I target it, maybe using javascript, jquery or php? Thanks!

Is there any way I can set the cursor position off the screen/webpage or on top/bottom of the page when a page is load? If so how can I target it, maybe using javascript, jquery or php? Thanks!

Share Improve this question edited Feb 28, 2012 at 19:53 Your Common Sense 158k42 gold badges224 silver badges365 bronze badges asked Feb 28, 2012 at 19:46 grumpypandagrumpypanda 5711 gold badge12 silver badges37 bronze badges 5
  • Can you please elaborate what you're trying to acplish? If you're trying to set an action for a 'hover' event, you can simply acplish that by faking the action in the backend .There should never be any reason to physically require the mouse to be located somewhere on page load for something to happen - because you can create that effect of whatever the 'mouse position' is triggering. – Ohgodwhy Commented Feb 28, 2012 at 19:54
  • @Ohgodwhy, the reason I want to move the mouse pointer off screen is because the page I'm creating is an a tag page, so people click anywhere next page will show. I don't want people to see the status bar address when they open the page until they move the mouse and interact with the page, that's why moving cursor off the screen came to my mind. Any suggestions? Thanks. – grumpypanda Commented Feb 28, 2012 at 20:06
  • I would use an overlay with a 100% transparent background. this will disallow anyone from clicking through to under the element, but will show all of the data beneath it. Then I would call the jquery .mousemove function, and remove the layover from the DOM. – Ohgodwhy Commented Feb 28, 2012 at 20:10
  • @Ohgodwhy, that sounds very good. Sorry I'm fairly new to the field. Is there any links/code/tutorials I can check out to learn this method please? Thanks. – grumpypanda Commented Feb 28, 2012 at 20:13
  • Sure. I'll put my response in an answer. – Ohgodwhy Commented Feb 28, 2012 at 20:15
Add a ment  | 

3 Answers 3

Reset to default 4

There's no way of setting the mouse position in Javascript. It envolves a serious security issue.
Imagine how chaotic it would be if any web site could take control over your mouse.

This response is based on the user having the need to avoid interacting with the page on load. It was not phrased correctly at first.

First, we'll need to create the layover. For this, we're going to use an ID of 'pageLayover'.

our CSS:

#pageLayover{
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
  background:transparent;
  z-index:9000;
  display:block;
}

Then our jQuery:

$(function(){
  $("body").append('<div id="pageLayover"></div>');
  $(document).mousemove(function(){ //capture mouse movement event
    $("#pageLayover").remove(); // remove our layover from the DOM
  });
});

That's the basics. You can tweak with it from there. Several users are constantly using their mouse so you may want to delay the use of the function until after a few seconds of the page load.

Good luck.

Editing to provide better Visibility to Solution

I agree with MyStream here, This is definitely not a finite solution. There's tweaking to be done, as I noted in the ment.

If you want this to also be a click function, you simply need to make it a click function.

$("#myClickElement").live('click', function(){
  $("body").append('<div id="pageLayover"></div>');
});

will append the pagelayover. If you want to move the scope of the remove to a click, you could use the normalized mousedown function, such as ->

$('#element').mousedown(function(event) {
  if(event.which == 1){ $("#pageLayover").remove(); 
});

You can set the focus to a specific input element. That way you can control what happens when the user next types some text. Google for "setfocus javascript"

But you can't control the mouse. The user controls the mouse.

发布评论

评论列表(0)

  1. 暂无评论