本文目錄一覽:
- 1、js鼠標拖動div
- 2、javascript 拖拽移動滾動條
- 3、js中如何拖動DIV中的圖片?
- 4、js中sortable怎麼獲取拖動的東西
- 5、js 自定義的cursor在鼠標移動的時候不斷閃爍,可行必追加50
- 6、怎麼用 javascript 實現拖拽
js鼠標拖動div
你的obj.style.left是獲取不到的因為該div沒有設置style屬性所以只要將樣式改為行內就行了
!DOCTYPE html
html lang=”en”
head
meta charset=”UTF-8″
title文哥討厭IE/title
/head
body
div id=”over” onmousedown=”down(this,event)” style=”width:100px;height:30px;background:red;position:absolute;left:20px;top:500px;” onmousemove=”move(this,event)” onmouseup=”seup(this,event)”
/div
script type=”text/javascript”
var posX,posY;
var downX,downY;
var mark=false;
function down(obj,event)
{
obj.style.cursor=”move”;
posX=obj.style.left;
posY=obj.style.top;
downX=event.clientX;
downY=event.clientY;
mark=true;
///alert(posX);
///alert(posY);
}
function move(obj,event)
{
var moveX=event.clientX;
var moveY=event.clientY;
if (mark) {
obj.style.left=parseInt(moveX) + parseInt(posX) – parseInt(downX) + “px”;
obj.style.top=parseInt(moveY) + parseInt(posY) – parseInt(downY)+ “px”;
}
}
function seup(obj,event)
{
if (mark) {
var moveX=event.clientX;
var moveY=event.clientY;
obj.style.left=parseInt(moveX) + parseInt(posX) – parseInt(downX)+ “px”;
obj.style.top=parseInt(moveY) + parseInt(posY) – parseInt(downY)+ “px”;
downX=moveX;
downY=moveY;
}
obj.style.cursor=”default”;
mark = false;
}
/script
/body
/html
javascript 拖拽移動滾動條
script type=”text/javascript”
var _y;
var scrolling=false;
document.onmousedown=function(e){
var e = window.event || e;
_y = e.clientY;
scrolling=true;
};
document.onmousemove = function(e){
if(!scrolling) return;
var e = window.event || e;
document.body.style.cursor=”move”;
var move=(e.clientY-_y)/10;
var scrollTop=document.body.scrollTop + document.documentElement.scrollTop;
window.scrollTo(0,scrollTop+move);
};
document.onmouseup=function(){
scrolling=false;
document.body.style.cursor=”default”;
};
/script
不足是會選中文字- -!
js中如何拖動DIV中的圖片?
實現思路:
①鼠標按下+鼠標移動 → 拖拽
②鼠標鬆開 → 無拖拽
③鼠標偏移 → 拖拽距離
用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
js中sortable怎麼獲取拖動的東西
是jquery吧
所有的事件回調函數都有兩個參數:event和ui,瀏覽器自有event對象,和經過封裝的ui對象
ui.helper – 表示sortable元素的JQuery對象,通常是當前元素的克隆對象
ui.position – 表示相對當前對象,鼠標的坐標值對象{top,left}
ui.offset – 表示相對於當前頁面,鼠標的坐標值對象{top,left}
ui.item – 表示當前拖拽的元素
ui.placeholder – 佔位符(如果有定義的話)
ui.sender – 當前拖拽元素的所屬sortable對象(僅當元素是從另一個sortable對象傳遞過來時有用)
·參數(參數名 : 參數類型 : 默認值)
appendTo : String : ‘parent’
Defines where the helper that moves with the mouse is being appended to during the drag (for example, to resolve overlap/zIndex issues).
初始:$(‘.selector’).sortable({ appendTo: ‘body’ });
獲取:var appendTo = $(‘.selector’).sortable(‘option’, ‘appendTo’);
設置:$(‘.selector’).sortable(‘option’, ‘appendTo’, ‘body’);
axis : String : false
如果有設置,則元素僅能橫向或縱向拖動。可選值:’x’, ‘y’
初始:$(‘.selector’).sortable({ axis: ‘x’ });
獲取:var axis = $(‘.selector’).sortable(‘option’, ‘axis’);
設置:$(‘.selector’).sortable(‘option’, ‘axis’, ‘x’);
cancel : Selector : ‘:input,button’
阻止排序動作在匹配的元素上發生。
初始:$(‘.selector’).sortable({ cancel: ‘button’ });
獲取:var cancel = $(‘.selector’).sortable(‘option’, ‘cancel’);
設置:$(‘.selector’).sortable(‘option’, ‘cancel’, ‘button’);
connectWith : Selector : false
允許sortable對象連接另一個sortable對象,可將item元素拖拽到另一個中。
初始:$(‘.selector’).sortable({ connectWith: ‘.otherlist’ });
獲取:var connectWith = $(‘.selector’).sortable(‘option’, ‘connectWith’);
設置:$(‘.selector’).sortable(‘option’, ‘connectWith’, ‘.otherlist’);
containment : Element, String, Selector : false
約束排序動作只能在一個指定的範圍內發生。可選值:DOM對象, ‘parent’, ‘document’, ‘window’, 或jQuery對象
初始:$(‘.selector’).sortable({ containment: ‘parent’ });
獲取:var containment = $(‘.selector’).sortable(‘option’, ‘containment’);
設置:$(‘.selector’).sortable(‘option’, ‘containment’, ‘parent’);
cursor : String : ‘auto’
定義在開始排序動作時,如果的樣式。
初始:$(‘.selector’).sortable({ cursor: ‘crosshair’ });
獲取:var cursor = $(‘.selector’).sortable(‘option’, ‘cursor’);
設置:$(‘.selector’).sortable(‘option’, ‘cursor’, ‘crosshair’);
cursorAt : Object : false
當開始移動時,鼠標定位在的某個位置上(最多兩個方向)。可選值:{ top, left, right, bottom }.
初始:$(‘.selector’).sortable({ cursorAt: ‘top’ });
獲取:var cursorAt = $(‘.selector’).sortable(‘option’, ‘cursorAt’);
設置:$(‘.selector’).sortable(‘option’, ‘cursorAt’, ‘top’);
delay : Integer : 0
以毫秒為單位,設置延遲多久才激活排序動作。此參數可防止誤點擊。
初始:$(‘.selector’).sortable({ delay: 500 });
獲取:var delay = $(‘.selector’).sortable(‘option’, ‘delay’);
設置:$(‘.selector’).sortable(‘option’, ‘delay’, 500);
distance : Integer : 1
決定至少要在元素上面拖動多少像素後,才正式觸發排序動作。
初始:$(‘.selector’).sortable({ distance: 30 });
獲取:var distance = $(‘.selector’).sortable(‘option’, ‘distance’);
設置:$(‘.selector’).sortable(‘option’, ‘distance’, 30);
dropOnEmpty : Boolean : true
是否允許拖拽到一個空的sortable對象中。
初始:$(‘.selector’).sortable({ dropOnEmpty: false });
獲取:var dropOnEmpty = $(‘.selector’).sortable(‘option’, ‘dropOnEmpty’);
設置:$(‘.selector’).sortable(‘option’, ‘dropOnEmpty’, false);
forceHelperSize : Boolean : false
If true, forces the helper to have a size.
初始:$(‘.selector’).sortable({ forceHelperSize: true });
獲取:var forceHelperSize = $(‘.selector’).sortable(‘option’, ‘forceHelperSize’);
設置:$(‘.selector’).sortable(‘option’, ‘forceHelperSize’, true);
forcePlaceholderSize : Boolean : false
If true, forces the placeholder to have a size.
初始:$(‘.selector’).sortable({ forcePlaceholderSize: true });
獲取:var forcePlaceholderSize = $(‘.selector’).sortable(‘option’, ‘forcePlaceholderSize’);
設置:$(‘.selector’).sortable(‘option’, ‘forcePlaceholderSize’, true);
grid : Array : false
將排序對象的item元素視為一個格子處理,每次移動都按一個格子大小移動,數組值:[x,y]
初始:$(‘.selector’).sortable({ grid: [50, 20] });
獲取:var grid = $(‘.selector’).sortable(‘option’, ‘grid’);
設置:$(‘.selector’).sortable(‘option’, ‘grid’, [50, 20]);
handle : Selector, Element : false
限制排序的動作只能在item元素中的某個元素開始。
初始:$(‘.selector’).sortable({ handle: ‘h2’ });
獲取:var handle = $(‘.selector’).sortable(‘option’, ‘handle’);
設置:$(‘.selector’).sortable(‘option’, ‘handle’, ‘h2’);
helper : String, Function : ‘original’
設置是否在拖拽元素時,顯示一個輔助的元素。可選值:’original’, ‘clone’
初始:$(‘.selector’).sortable({ helper: ‘clone’ });
獲取:var helper = $(‘.selector’).sortable(‘option’, ‘helper’);
設置:$(‘.selector’).sortable(‘option’, ‘helper’, ‘clone’);
items : Selector : ‘ *’
指定在排序對象中,哪些元素是可以進行拖拽排序的。
初始:$(‘.selector’).sortable({ items: ‘li’ });
獲取:var items = $(‘.selector’).sortable(‘option’, ‘items’);
設置:$(‘.selector’).sortable(‘option’, ‘items’, ‘li’);
opacity : Float : false
定義當排序時,輔助元素(helper)顯示的透明度。
初始:$(‘.selector’).sortable({ opacity: 0.6 });
獲取:var opacity = $(‘.selector’).sortable(‘option’, ‘opacity’);
設置:$(‘.selector’).sortable(‘option’, ‘opacity’, 0.6);
placeholderType: StringDefault: false
設置當排序動作發生時,空白佔位符的CSS樣式。
初始:$(‘.selector’).sortable({ placeholder: ‘ui-state-highlight’ });
獲取:var placeholder = $(‘.selector’).sortable(‘option’, ‘placeholder’);
設置:$(‘.selector’).sortable(‘option’, ‘placeholder’, ‘ui-state-highlight’);
revert : Boolean : false
如果設置成true,則被拖拽的元素在返回新位置時,會有一個動畫效果。
初始:$(‘.selector’).sortable({ revert: true });
獲取:var revert = $(‘.selector’).sortable(‘option’, ‘revert’);
設置:$(‘.selector’).sortable(‘option’, ‘revert’, true);
scroll : Boolean : true
如果設置成true,則元素被拖動到頁面邊緣時,會自動滾動。
初始:$(‘.selector’).sortable({ scroll: false });
獲取:var scroll = $(‘.selector’).sortable(‘option’, ‘scroll’);
設置:$(‘.selector’).sortable(‘option’, ‘scroll’, false);
scrollSensitivity : Integer : 20
設置當元素移動至邊緣多少像素時,便開始滾動頁面。
初始:$(‘.selector’).sortable({ scrollSensitivity: 40 });
獲取:var scrollSensitivity = $(‘.selector’).sortable(‘option’, ‘scrollSensitivity’);
設置:$(‘.selector’).sortable(‘option’, ‘scrollSensitivity’, 40);
scrollSpeed : Integer : 20
設置頁面滾動的速度。
初始:$(‘.selector’).sortable({ scrollSpeed: 40 });
獲取:var scrollSpeed = $(‘.selector’).sortable(‘option’, ‘scrollSpeed’);
設置:$(‘.selector’).sortable(‘option’, ‘scrollSpeed’, 40);
tolerance : String : ‘intersect’
設置當拖動元素越過其它元素多少時便對元素進行重新排序。可選值:’intersect’, ‘pointer’
intersect:至少重疊50%
pointer:鼠標指針重疊元素
初始:$(‘.selector’).sortable({ tolerance: ‘pointer’ });
獲取:var tolerance = $(‘.selector’).sortable(‘option’, ‘tolerance’);
設置:$(‘.selector’).sortable(‘option’, ‘tolerance’, ‘pointer’);
zIndex : Integer : 1000
設置在排序動作發生時,元素的z-index值。
初始:$(‘.selector’).sortable({ zIndex: 5 });
獲取:var zIndex = $(‘.selector’).sortable(‘option’, ‘zIndex’);
設置:$(‘.selector’).sortable(‘option’, ‘zIndex’, 5);
·事件
start
當排序動作開始時觸發此事件。
定義:$(‘.selector’).sortable({ start: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortstart’, function(event, ui) { … });
sort
當元素髮生排序時觸發此事件。
定義:$(‘.selector’).sortable({ sort: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sort’, function(event, ui) { … });
change
當元素髮生排序且坐標已發生改變時觸發此事件。
定義:$(‘.selector’).sortable({ change: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortchange’, function(event, ui) { … });
beforeStop
當排序動作結束之前觸發此事件。此時佔位符元素和輔助元素仍有效。
定義:$(‘.selector’).sortable({ beforeStop: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortbeforeStop’, function(event, ui) { … });
stop
當排序動作結束時觸發此事件。
定義:$(‘.selector’).sortable({ stop: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortstop’, function(event, ui) { … });
update
當排序動作結束時且元素坐標已經發生改變時觸發此事件。
定義:$(‘.selector’).sortable({ update: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortupdate’, function(event, ui) { … });
receive
當一個已連接的sortable對象接收到另一個sortable對象的元素後觸發此事件。
定義:$(‘.selector’).sortable({ receive: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortreceive’, function(event, ui) { … });
over
當一個元素拖拽移入另一個sortable對象後觸發此事件。
定義:$(‘.selector’).sortable({ over: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortover’, function(event, ui) { … });
out
當一個元素拖拽移出sortable對象移出並進入另一個sortable對象後觸發此事件。
定義:$(‘.selector’).sortable({ out: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortout’, function(event, ui) { … });
activate
當一個有使用連接的sortable對象開始排序動作時,所有允許的sortable觸發此事件。
定義:$(‘.selector’).sortable({ activate: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortactivate’, function(event, ui) { … });
deactivate
當一個有使用連接的sortable對象結束排序動作時,所有允許的sortable觸發此事件。
定義:$(‘.selector’).sortable({ deactivate: function(event, ui) { … } });
綁定:$(‘.selector’).bind(‘sortdeactivate’, function(event, ui) { … });
·方法
destory
從元素中移除拖拽功能。
用法:.sortable( ‘destroy’ )
disable
禁用元素的拖拽功能。
用法:.sortable( ‘disable’ )
enable
啟用元素的拖拽功能。
用法:.sortable( ‘enable’ )
option
獲取或設置元素的參數。
用法:.sortable( ‘option’ , optionName , [value] )
serialize
獲取或設置序列化後的每個item元素的id屬性。
用法:.sortable( ‘serialize’ , [options] )
toArray
獲取序列化後的每個item元素的id屬性的數組。
用法:.sortable( ‘toArray’ )
refresh
手動重新刷新當前sortable對象的item元素的排序。
用法:.sortable( ‘refresh’ )
refreshPositions
手動重新刷新當前sortable對象的item元素的坐標,此方法可能會降低性能。
用法:.sortable( ‘refreshPositions’ )
cancel
取消當前sortable對象中item元素的排序改變。
用法:.sortable( ‘cancel’ )
排序後保存有兩種方法,一是cookie,二是數據庫配置文件等。
下面是數據庫的部分代碼 原作:
複製代碼代碼如下:
$(function() {
var show = $(“.loader”);
var orderlist = $(“.orderlist”);
var listleft = $(“div[id = ‘column_left’]”);
var listcenter = $(“div[id = ‘column_center’]”);
var listright = $(“div[id = ‘column_right’]”);
$( “.column” ).sortable({
opacity: 0.5,//拖動的透明度
revert: true, //緩衝效果
cursor: ‘move’, //拖動的時候鼠標樣式
connectWith: “.column”,
//開始用update 結果執行兩次,浪費資源,古改成stop
//但是stop在元素沒有改變位置的時候也會執行,
//用update其他js會有問題,^_^
stop: function(){
var new_order_left = []; //左欄布局
var new_order_center = [];//中欄布局
var new_order_right = [];//右欄布局
listleft.children(“.portlet”).each(function() {
new_order_left.push(this.title);
});
listcenter.children(“.portlet”).each(function() {
new_order_center.push(this.title);
});
listright.children(“.portlet”).each(function() {
new_order_right.push(this.title);
});
var newleftid = new_order_left.join(‘,’);
var newcenterid = new_order_center.join(‘,’);
var newrightid = new_order_right.join(‘,’);
$.ajax({
type: “post”,
url: jsonUrl, //服務端處理程序
data: { leftid: newleftid, centerid: newcenterid, rightid:newrightid}, //id:新的排列對應的ID,order:原排列順序
// beforeSend: function() {
// show.html(” 正在更新”);
// },
success: function(msg) {
//alert(msg);
show.html(“”);
}
});
}
});
js 自定義的cursor在鼠標移動的時候不斷閃爍,可行必追加50
body bgColor=#000
SCRIPT language=JavaScript
!–
//To add more stars simply add more colours in below array!!
colours=new Array(‘ff0000′,’00ff00′,’3366ff’,’ff00ff’,’ffa500′,’ffffff’,’fff000′)
//Alter nothing below!!
amount=colours.length;
YgetDelay=0,XgetDelay=0,Ydelay=0,Xdelay=0,ns=(document.layers)?1:0,step=0.2,currStep=0,my=0,mx=0;
if (ns){
for (i=0; i amount; i++)
document.write(‘LAYER NAME=”nsstars’+i+'” BGCOLOR=’+colours[i]+’ CLIP=”0,0,2,2″/LAYER’);
}
else{
document.write(‘div id=”ie” style=”position:absolute;top:0;left:0;”div style=”position:relative”‘);
for (i=0; i amount; i++)
document.write(‘span id=”iestars” style=”position:absolute;top:0;left:0;width:2px;height:2px;background:’+colours[i]+’;font-size:2px”/span’);
document.write(‘/div/div’);
}
if (ns){
window.captureEvents(Event.MOUSEMOVE);
function nMouse(evnt){
my=evnt.pageY;mx=evnt.pageX
}
window.onMouseMove=nMouse;
}
else{
function iMouse(){
my=event.y;mx=event.x;
}
document.onmousemove=iMouse
}
function stars(){
if (!ns)ie.style.top=document.body.scrollTop;
for (i=0; i amount; i++)
{
var layer=(document.layers)?document.layers[“nsstars”+i]:iestars[i].style;
layer.top= Ydelay+100*Math.sin((5*Math.sin((currStep-15.99)/10))+i*70)*Math.sin((currStep)/10)*Math.cos((currStep+i*25)/10);
layer.left=Xdelay+180*Math.cos((5*Math.sin((currStep-15.99)/10))+i*70)*Math.sin((currStep)/10)*Math.cos((currStep+i*25)/10);
}
currStep+=step;
}
function delay(){
Ydelay = YgetDelay+=(my-YgetDelay)*1/20;
Xdelay = XgetDelay+=(mx-XgetDelay)*1/20;
stars();
setTimeout(‘delay()’,10);
}
delay();
//–
/SCRIPT
/body
複製,保存*.html就行
怎麼用 javascript 實現拖拽
javascript 實現拖拽實現原理。如下:
用JavaScript事件方法表示就是:
① onmousedown + onmousemove → startDrag()
② onmouseup → stopDrag()
1.首先調用js文件,如下:
script src=”” type=”text/javascript”/script
2.然後使用startDrag()方法綁定拖拽效果,startDrag()方法有兩個參數,第一個是點擊的對象(即點擊那裡可以實現拖拽,例如彈出層的標題欄),第二個是拖拽的對象(例如一個彈出層)。也就是startDrag(觸發拖拽對象,被拖拽對象)。
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=”” type=”text/javascript”/script
script type=”text/javascript”
var oBox = document.getElementById(“box”);
var oBar = document.getElementById(“bar”);
startDrag(oBar, oBox);
/script
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/291851.html