~~~
function drag(target, obj) {
//target:鼠標按下的目標;obj:要拖動的整體對象(兩者須原生獲取)
target.onmousedown = function(ev) {
var ev = ev || window.event;
var disX = ev.clientX - obj.offsetLeft;
var disY = ev.clientY - obj.offsetTop;
//鼠標移動事件
document.onmousemove = function(ev) {
var ev = ev || window.event;
obj.style.left = ev.clientX - disX + 'px';
obj.style.top = ev.clientY - disY + 'px';
}
//鼠標抬起事件
document.onmouseup = function() {
document.onmousemove = null;
}
}
~~~
#### 邊框拖拽
* * * * *
~~~
var m = 0;
function drag(target, obj,obj_border) {
//target:鼠標按下的目標;obj:鼠標抬起時,瞬移的對象;obj_border:要拖動的邊框對象(三者須原生獲取)
target.onmousedown = function(ev) {
if(ev.target == target){
//按下時,顯示邊框
search_bar_border.style.display = 'block';
var ev = ev || window.event;
var disX = ev.clientX - obj.offsetLeft;
var disY = ev.clientY - obj.offsetTop;
//鼠標拖動時,邊框跟隨移動
document.onmousemove = function(ev) {
m = 1;
var ev = ev || window.event;
obj_border.style.left = ev.clientX - disX + 'px';
obj_border.style.top = ev.clientY - disY + 'px';
}
//鼠標抬起時,讓obj瞬間移到邊框位置,隱藏邊框
document.onmouseup = function(ev) {
var ev = ev || window.event;
//m用于判斷鼠標是否移動,排除直接點擊瞬移的可能
if(m == 1){
m = 0;
obj.style.left = ev.clientX - disX + 'px';
obj.style.top = ev.clientY - disY + 'px';
}
search_bar_border.style.display = 'none';
document.onmousemove = null;
}
}
}
}
~~~
- 我的爛筆頭
- 1、常用功能方法整合
- 2、jQuery基本函數
- 3、在頁面中添加圖片
- 4、精度算法
- 5、圖片懶加載
- 6、彈窗拖拽功能
- 7、彈幕功能
- 8、鼠標滾動事件
- 9、獲取頁面相關屬性
- 10、彈窗的三種展現方式
- 11、輪播功能
- 12、獲取唯一標識
- 13、CSS樣式效果
- 14、任意兩點的動態連線
- 15、全新接口功能
- 16、適配兼容
- 17、無刷新頁面更改URL
- 18、定時器的那些事
- 19、關于iframe的常見問題
- 20、設置不同的時間
- 21、關于select-option
- 22、省市級聯
- 23、省市級聯數據
- 24、關于數據傳輸問題
- 25、問題分支
- 那些年我們一起走過的神坑
- 1、關于console的使用
- 2、關于數組
- 1、數組的賦值
- 2、數組的常用方法
- 3、關于移動端的bug
- 4、關于視頻的bug
- 5、那些坑坑洼洼
- 6、關于字符串
- 1、字符串的常用方法
- 頁面布局
- 1、背景固定的滾動頁面
- 心得
- Node.js
- 1、安裝環境
- ThinkPHP 5.1
- 1、訪問格式