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

javascript - Prevent Image Drag Selection - Stack Overflow

programmeradmin1浏览0评论

I have looked all over the internet, but have found nothing that works.

I want my image not to be selected, like a div with a background image (<div style = "background-image : 'moo.png';"> </div>)

I can't use a background image, because I need an image map for functionality.

my code looks like this:

css,

.filmmakers #map {

-moz-user-select : -moz-none;
-khtml-user-select : none;
-webkit-user-select : none;
-ms-user-select : none;
-o-user-select : none;
user-select : none;

}

.filmmakers #map::selection {

   color : rgba(0, 0, 0, 0);

}

html,

<img id = "map" src = "WorldMap.png" alt = "Map" unselectable = "on" style = "left : 0px; top : 0px;" />

javascript,

var map = document.getElementById('map');

makeUnselectable(map);

function makeUnselectable(node) {

if (node.nodeType == 1) {

   node.setAttribute("unselectable", "on");

}

   var child = node.firstChild;

while (child) {

   makeUnselectable(child);
   child = child.nextSibling;

}

}

As you can see, I still get the drag icon and it messes with the functionality of the map:

.html

Please help and thanks for taking the time to read my post, I really appreciate your kind attention.

Cheers!

Mithos

I have looked all over the internet, but have found nothing that works.

I want my image not to be selected, like a div with a background image (<div style = "background-image : 'moo.png';"> </div>)

I can't use a background image, because I need an image map for functionality.

my code looks like this:

css,

.filmmakers #map {

-moz-user-select : -moz-none;
-khtml-user-select : none;
-webkit-user-select : none;
-ms-user-select : none;
-o-user-select : none;
user-select : none;

}

.filmmakers #map::selection {

   color : rgba(0, 0, 0, 0);

}

html,

<img id = "map" src = "WorldMap.png" alt = "Map" unselectable = "on" style = "left : 0px; top : 0px;" />

javascript,

var map = document.getElementById('map');

makeUnselectable(map);

function makeUnselectable(node) {

if (node.nodeType == 1) {

   node.setAttribute("unselectable", "on");

}

   var child = node.firstChild;

while (child) {

   makeUnselectable(child);
   child = child.nextSibling;

}

}

As you can see, I still get the drag icon and it messes with the functionality of the map:

https://dl.dropbox./u/107533178/CampusRoyal/Filmmakers.html

Please help and thanks for taking the time to read my post, I really appreciate your kind attention.

Cheers!

Mithos

Share Improve this question edited Nov 21, 2012 at 13:12 MithosAnnar asked Nov 21, 2012 at 13:05 MithosAnnarMithosAnnar 693 silver badges9 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 11

You need:

window.ondragstart = function() { return false; } 

If you are using jQuery

$("img").mousedown(function(){
    return false;
});

Answer here : https://stackoverflow./a/11135622/1722914

User this css :

-moz-user-select: none;
-webkit-user-select: none;
user-select: none;
发布评论

评论列表(0)

  1. 暂无评论