js中如何拖动DIV中的图片?

JavaScript014

js中如何拖动DIV中的图片?,第1张

实现思路:

①鼠标按下+鼠标移动 → 拖拽

②鼠标松开 → 无拖拽

③鼠标偏移 → 拖拽距离

用JavaScript事件方法表示就是:

① onmousedown + onmousemove → startDrag()

② onmouseup → stopDrag()

drag.js代码:

var params = {

left: 0,

top: 0,

currentX: 0,

currentY: 0,

flag: false

}

//获取相关CSS属性

var getCss = function(o,key){

return o.currentStyle? o.currentStyle[key] : document.defaultView.getComputedStyle(o,false)[key] 

}

//拖拽的实现

var startDrag = function(bar, target, callback){

if(getCss(target, "left") !== "auto"){

params.left = getCss(target, "left")

}

if(getCss(target, "top") !== "auto"){

params.top = getCss(target, "top")

}

//o是移动对象

bar.onmousedown = function(event){

params.flag = true

if(!event){

event = window.event

//防止IE文字选中

bar.onselectstart = function(){

return false

}  

}

var e = event

params.currentX = e.clientX

params.currentY = e.clientY

}

document.onmouseup = function(){

params.flag = false

if(getCss(target, "left") !== "auto"){

params.left = getCss(target, "left")

}

if(getCss(target, "top") !== "auto"){

params.top = getCss(target, "top")

}

}

document.onmousemove = function(event){

var e = event ? event: window.event

if(params.flag){

var nowX = e.clientX, nowY = e.clientY

var disX = nowX - params.currentX, disY = nowY - params.currentY

target.style.left = parseInt(params.left) + disX + "px"

target.style.top = parseInt(params.top) + disY + "px"

}

if (typeof callback == "function") {

callback(parseInt(params.left) + disX, parseInt(params.top) + disY)

}

}

}

HTML/CSS

<style type="text/css">

#box{position:absolute left:100px top:100px padding:5px background:#f0f3f9 font-size:12px -moz-box-shadow:2px 2px 4px #666666 -webkit-box-shadow:2px 2px 4px #666666}

#main{border:1px solid #a0b3d6 background:white}

#bar{line-height:24px background:#beceeb border-bottom:1px solid #a0b3d6 padding-left:5px cursor:move}

#content{width:420px height:250px padding:10px 5px}

</style>

<div id="box">

    <div id="main">

        <div id="bar">拖拽</div>

        <div id="content">

            内容……

        </div>

    </div>

</div>

JS部分

<script src="drag.js" type="text/javascript"></script>

<script type="text/javascript">

    var oBox = document.getElementById("box")

    var oBar = document.getElementById("bar")

    startDrag(oBar, oBox)

</script>

不是很简单一两句话能说清楚地,需要HTML, CSS和javascript的配合。我大概说下做法,你需要自己写写试试看

首先把你的div设置style为overflow hidden

然后在div里面再添加一个div,假设叫做imgcontainer,在里面存放所有图片,图片以float: left方式显示,每个图片的宽度你是知道的,这样图片的个数*每个图片的宽度(必要的话再加上图片之间的间隔)设置为imgcontainer的总宽度。然后把div的宽度设置为显示3个图片的宽度。这样就可以保证只显示3个图片,多余的图片看不到。

然后在点击左右2个按钮的时候添加事件,每点击一次,让imgcontainer的marginLeft增加或减少一个图片的宽度(同样必要的时候加上图片之间的间隔距离),这样就使得图片列表在左右移动了。

下面是我的一个例子,跟你的需求可能不完全一样,大概做下参考:

HTML片段

<div id="pagecontent" class="pagecontent">

<div id="nav_thumbs_prev" class="disabled" onclick="NavThumbsPrev()"></div>

<div id="photolist_container">

<ol id="photolist" class="photolist">

<li><figure onclick="ViewPhoto(this,'aaaaa')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">aaaaa</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'bbbbb')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">bbbbb</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'cccc')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">cccc</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'dddd')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">dddd</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'eeee')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">eeee</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'ffff')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">ffff</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'gggg')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">gggg</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'hhhh')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">hhhh</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'iiii')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">iiii</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'jjjj')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">jjjj</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'kkkk')"><div class="samplecontainer"><img src="./img/newborn.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">kkkk</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'llll')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">llll</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'mmmm')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">mmmm</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'nnnn')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">nnnn</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'oooo')"><div class="samplecontainer"><img src="./img/singularity.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">oooo</figcaption></figure></li>

<li><figure onclick="ViewPhoto(this,'pppp')"><div class="samplecontainer"><img src="./img/pyre.jpg" onload="javascript:DrawSampleImage(this)" border="0" ></div><figcaption class="imagename">pppp</figcaption></figure></li>

</ol>

</div>

<div id="nav_thumbs_next" onclick="NavThumbsNext()"></div>

</div>

CSS部分

.photolist_container {

margin: 0

/*Firefox*/

width: -moz-calc(100% - 30px)

/*chrome safari*/

width: -webkit-calc(100% - 30px)

/*Standard */

width: calc(100% - 30px)

float: left

height: 140px

overflow: hidden

}

ol.photolist {

background-color: black

margin: 0

margin-left: 1px

padding: 0

list-style: none

height: auto

padding: 0

display: block

height: 125px

}

ol.photolist>li {

margin: 0

margin-left: -1px

padding: 4px

text-align:center

display:inline

float: left

border: 1px solid #FF6699

width: 100px

}

#photolist figure {

margin: 0

padding: 0

text-align:center

border: 1px solid #000000

cursor: pointer

}

.samplecontainer {

height: 100px

width: 100px

text-align: center

margin: 0

padding: 0

border: 0

display: table-cell

vertical-align: middle

}

.samplecontainer>img {

margin: 0

padding: 0

border: 0

vertical-align:middle

}

Javascript部分

function MovePhotoList(size) {

var photolist = document.getElementById("photolist")

var marginLeft = 1

if (photolist.style.marginLeft != "")

marginLeft = parseInt(photolist.style.marginLeft)

marginLeft = marginLeft + size

if (marginLeft <(document.getElementById("photolist_container").offsetWidth - photolist.offsetWidth))

{

marginLeft = document.getElementById("photolist_container").offsetWidth - photolist.offsetWidth

document.getElementById("nav_thumbs_next").setAttribute("class", "disabled")

}

else

{

document.getElementById("nav_thumbs_next").setAttribute("class", "enable")

}

if (marginLeft >1)

{

marginLeft = 1

document.getElementById("nav_thumbs_prev").setAttribute("class", "disabled")

}

else

{

document.getElementById("nav_thumbs_prev").setAttribute("class", "enable")

}

photolist.style.marginLeft = marginLeft + "px"

}

function NavThumbsPrev() {

var nav_thumbs_prev = document.getElementById("nav_thumbs_prev")

if (nav_thumbs_prev.getAttribute("class") != "disabled") {

MovePhotoList(56)

}

}

function NavThumbsNext() {

var nav_thumbs_next = document.getElementById("nav_thumbs_next")

if (nav_thumbs_next.getAttribute("class") != "disabled") {

MovePhotoList(-56)

}

}