0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【JavaScript】マウス

Last updated at Posted at 2019-06-01

要素内のマウスの位置を調べる

概要

要素内のマウスの位置を調べる

スクリプト

function getpos(e){
    var isFirefox,pageX,pageY;
    var isTouch = ('ontouchstart' in window);
    if(navigator.userAgent.indexOf('Firefox')!=-1){
        isFirefox = true;
    }else{
        isFirefox = false;
    }

    if(isTouch){
        if('originalEvent' in e){
            if('pageX' in e.originalEvent){
                pageX = e.originalEvent.pageX;
                pageY = e.originalEvent.pageY;
            }else if('touches' in e.originalEvent){
                pageX = e.originalEvent.touches[0].pageX;
                pageY = e.originalEvent.touches[0].pageY;
            }

        }else if('changedTouches' in event){
            pageX = event.changedTouches[0].pageX;
            pageY = event.changedTouches[0].pageY;
        }else  if('pageX' in e){
            pageX = e.pageX;
            pageY = e.pageY;
        }

    }else{
        pageX = e.pageX;
        pageY = e.pageY;
    }

    return {
        pageX : Math.round(pageX),
        pageY : Math.round(pageY)
    }
}

右クリック禁止

概要

右クリックでコンテキストメニューを表示させたくない場合

施策

右クリック禁止

document.addEventListener('contextmenu', function (e) {
    e.preventDefault();
}, false);

特定のタグのみ禁止

document.addEventListener('contextmenu', function (e) {
    var tagName = e.target.tagName.toLowerCase();
    if(tagName==="img") { //imgタグ上で右クリック禁止
        e.preventDefault();
    }
}, false);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?