您的位置:首页 > Web前端 > JQuery

jquery鼠标随意移动div

2015-06-30 10:17 671 查看
<!DOCTYPE html>

<html lang="zh">

<head>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<title>Qing's Web</title>

<script src="//cdn.bootcss.com/jquery/1.8.0/jquery.js"></script>

<style type="text/css">

.footer {

position: fixed;

bottom: 0;

width: 100%;

}

.moveBar {

position: absolute;

width: 250px;

height: 300px;

background: #666;

border: solid 1px #000;

}

#banner {

background: #52CCCC;

cursor: move;

}

</style>

</head>

<body style="padding-top: 50px;">

<div class="moveBar">

<div id="banner">按住此处移动当前div</div>

<div class="content">这里是其它内容</div>

</div>

<script>

jQuery(document).ready( function () {

$('#banner').mousedown( function (event) {

var isMove = true;

var abs_x = event.pageX - $('div.moveBar').offset().left;

var abs_y = event.pageY - $('div.moveBar').offset().top;

$(document).mousemove(function (event) {

if (isMove) {

var obj = $('div.moveBar');

obj.css({'left':event.pageX - abs_x, 'top':event.pageY - abs_y});

}

}).mouseup( function () {

isMove = false;

});

});

});

</script>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: