jquery 获取鼠标和元素的坐标点
生活随笔
收集整理的這篇文章主要介紹了
jquery 获取鼠标和元素的坐标点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取當前鼠標相對img元素的坐標 [javascript]?view plaincopy $('img').mousemove(function(e)?{?? ?????varpositionX=e.pageX-$(this).offset().left;?//獲取當前鼠標相對img的X坐標?? ?????varpositionY=e.pageY-$(this).offset().top;?//獲取當前鼠標相對img的Y坐標?? ?????console.log(positionX+'???'+positionY);?? ?})?? 獲取當前鼠標相對瀏覽器的原點的坐標?????? [javascript]?view plaincopy $('img').mousemove(function(e)?{?????? ????var?xx?=?e.originalEvent.x?||e.originalEvent.layerX?||?0;?????? ????var?yy?=?e.originalEvent.y?||e.originalEvent.layerY?||?0;??????????? ????console.log(xx+'???'+yy);???????????? })?? 獲取當前鼠標相對html頁面的原點的坐標????????? [javascript]?view plaincopy $('img').mousemove(function(e)?{?? ????var?pageX=e.pageX;?????????? ????var?pageY=e.pageY;????????? ????console.log(pageX+'???'+pageY);?????????? })?? <scripttype="text/javascript">?? $(document).ready(function(){?? ?????????alert($(window).height());//瀏覽器當前窗口可視區域高度?? ????????alert($(document).height());//瀏覽器當前窗口文檔的高度?? ????????alert($(document.body).height());//瀏覽器當前窗口文檔body的高度?? ????????alert($(document.body).outerHeight(true));//瀏覽器當前窗口文檔body的總高度包括border?padding?margin?? ????????alert($(window).width());//瀏覽器當前窗口可視區域寬度?? ????????alert($(document).width());//瀏覽器當前窗口文檔對象寬度?? ????????alert($(document.body).width());//瀏覽器當前窗口文檔body的高度?? ????????alert($(document.body).outerWidth(true));//瀏覽器當前窗口文檔body的總寬度包括border?padding?margin?? })?? </script> ??
1,獲取對象
var obj = $("#image");???????????????
2,獲取對象元素的位置(offset()方法)
var offset = obj.offset();
獲取對象元素的位置,分別是元素的top和left,調用方法是:offset.left和offset.top,可知當前對象的左部和頂部位置。??
3,獲取對象元素的寬度(width()方法)
var right = offset.left+obj.width();
實例中是獲取對象的右下角位置,創建新窗口的左部位置。
4,獲取對象元素的高度(height()方法)
???var down =offset.top+obj.height();
實例中是獲取對象的右下角位置,創建新窗口的頂部位置。??????????????????????????????
轉載于:https://www.cnblogs.com/taleche/p/6065545.html
總結
以上是生活随笔為你收集整理的jquery 获取鼠标和元素的坐标点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微PE制作U盘启动盘图文详细教程
- 下一篇: 《21天学通C语言(第7版)》一2.4