css图片drag时拖动了一片

html-css019

css图片drag时拖动了一片,第1张

css怎样防止图片被拖拽一片

在浏览器中,常见的一种行为就是:选中-拖拽-新页面打开,例如百度搜索就是这样.但是我现在不想让别人在我的个人网页上选中,也不想让他人拖拽我的照片,要怎么做呢?

下面我们来看一下使用css设置图片不可拖动的方法。

css设置图片不可拖动实例:

css居中对齐

div{

width: 500px

height: 500px

border: 1px solid red}

img {

-webkit-user-drag: none

}

css设置图片不可拖动主要使用user-drag来设置。user-drag设置或检索一个元素可以被拖拽。

法:

user-drag:auto | element | none

默认值:auto

适用于:所有元素

继承性:有

动画性:否

计算值:指定值

取值:

auto:使用默认的拖拽行为,这种情况只有图片和链接可以被拖拽。

element:整个元素而非它的内容可拖拽。

none:元素不能被拖动。在通过选中后可拖拽。

dragmove 是 jquery 的插件吧,引用 js 文件后直接在页面中给要拖拉的元素添加 class 或者 id 就可以了,不需要修改 CSS,具体看 dragmove 的样例文档

以下直接可以运行。

如果DIV在ID为area的div中,即可拖拽。我在Drag函数里做了对父div的判断。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>鼠标拖拽</title>

<script type="text/javascript">

function Drag(o, e){

if(o.parentElement!=document.getElementById('area')) return

var e = window.event || e

var _x = e.offsetX || e.layerX

var _y = e.offsetY || e.layerY

o.style.filter = 'Alpha(opacity=70)'

o.style.opacity = '0.7'

document.onmousemove = function(e){

var e = window.event || e

o.style.left = e.clientX - _x + 'px'

o.style.top = e.clientY - _y + 'px'

o.style.cursor="move"

}

document.onmouseup = function(e){

document.onmousemove = null

o.style.filter = o.style.opacity = ''

o.style.cursor="default"

}

}

</script>

</head>

<body>

<div id='area'>

<div onmousedown="Drag(this, event)" style="position:absoluteborder:1px solid redbackground:pinkwidth:400pxheight:300px"></div>

</div>

<div onmousedown="Drag(this, event)" style="position:absoluteleft:500pxborder:1px solid redbackground:pinkwidth:400pxheight:300px"></div>

</body>

</html>