久久久久在线观看_又色又爽又黄的免费视频播放_一区中文字幕_日韩电影在线播放

今日焦點 焦點資訊 營銷之道 企業報道 淘寶運營 網站建設 軟件開發 400電話
  當前位置: 首頁 » 資訊 » 軟件開發 » 正文

js實現拖拽和吸附代碼分享

放大字體  縮小字體 發布日期:2018-03-02  來源:企業800網  作者:新格網  瀏覽次數:35  【去百度看看】
核心提示:本文主要和大家分享js實現拖拽和吸附代碼,希望能幫助到大家。
本文主要和大家分享js實現拖拽和吸附代碼,希望能幫助到大家。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>document</title>
<style>
* {
padding: 0;
margin: 0;
}
#big {
width: 500px;
height: 500px;
background-color: #ccc;
position: relative
}
#box {
width: 100px;
height: 100px;
background-color: #f00;
position: absolute
}
</style>
<script>
window.onload = function () {
var box = document.getElementById('box');
var big = document.getElementById('big');
// 鼠標在box中的位置
var disX = 0,
disY = 0;
box.onmousedown = function (e) {
var thisE = e || event;
disX = thisE.clientX - box.offsetLeft;
disY = thisE.clientY - box.offsetTop;
document.onmousemove = function (ev) {
var thisEvent = ev || event;
var l = thisEvent.clientX - disX;
var t = thisEvent.clientY - disY;
if (l < 20) {
l = 0;
} else if (l > big.offsetWidth - box.offsetWidth - 20) {
l = big.offsetWidth - box.offsetWidth;
}
if (t < 20) {
t = 0;
} else if (t > big.offsetHeight - box.offsetHeight - 20) {
t = big.offsetHeight - box.offsetHeight;
}
box.style.left = l + 'px';
box.style.top = t + 'px';
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}
}
e.preventDefault();
}
}
</script>
</head>
<body>
<p id="big">
<p id="box"></p>
</p>
</body>
</html>

相關推薦:

js代碼實現鼠標拖拽p實例

js控制文件拖拽及獲取拖拽內容

限制范圍拖拽,磁性吸附。

以上就是js實現拖拽和吸附代碼分享的詳細內容,更多請關注php中文網其它相關文章!

 
 
[ 資訊搜索 ]  [ 加入收藏 ]  [ 告訴好友 ]  [ 打印本文 ]  [ 違規舉報 ]  [ 關閉窗口 ]

 
0條 [查看全部]  相關評論

 
網站首頁 | 關于我們 | 聯系方式 | 使用協議 | 版權隱私 | 網站地圖 | 排名推廣 | 廣告服務 | 積分換禮 | 網站留言 | RSS訂閱 | 皖ICP備2021004516號-14
企業800網 · 提供技術支持