日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html 拖拽坐标,Html+css实现拖拽导航条

發(fā)布時(shí)間:2025/3/8 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html 拖拽坐标,Html+css实现拖拽导航条 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
div橫向拖拽排序

body, div {

padding: 0px;

margin: 0px;

}

.box {

position: relative;

margin-left: 15px;

padding: 10px;

padding-right: 0px;

width: 810px;

border: blue solid 1px;

}

.box ul{

list-style: none;

overflow: hidden;

padding: 0;

margin:0;

}

.drag {

float: left;

border: #000 solid 1px;

text-align: center;

}

.box ul li a{

display: block;

padding: 10px 25px;

}

.drag-dash {

position: absolute;

border: #000 solid 1px;

background: #ececec;

}

.dash {

float: left;

border: 1px solid transparent;

}

div橫向拖拽排序

  • 導(dǎo)航一
  • 導(dǎo)航二導(dǎo)航
  • 導(dǎo)航導(dǎo)航導(dǎo)航三
  • 導(dǎo)航導(dǎo)航四
  • 導(dǎo)五

$(document).ready(function () {

var range = {x: 0, y: 0};//鼠標(biāo)元素偏移量

var lastPos = {x: 0, y: 0, x1: 0, y1: 0}; //拖拽對(duì)象的四個(gè)坐標(biāo)

var tarPos = {x: 0, y: 0, x1: 0, y1: 0}; //目標(biāo)元素對(duì)象的坐標(biāo)初始化

var theDiv = null, move = false;

var choose = false; //拖拽對(duì)象拖拽狀態(tài)選中狀態(tài)

var theDivId = 0, theDivHeight = 0, theDivHalf = 0;

var tarFirstY = 0; //拖拽對(duì)象的索引、高度、的初始化。

var tarDiv = null, tarFirst, tempDiv; //要插入的目標(biāo)元素的對(duì)象,臨時(shí)的虛線對(duì)象

var initPos = {x: 0, y: 0};

var theDivWidth;//拖拽對(duì)象的寬度

$(".drag").each(function () {

$(this).mousedown(function (event) {

choose = true;

//拖拽對(duì)象

theDiv = $(this);

//記錄拖拽元素初始位置

initPos.x = theDiv.position().left;

initPos.y = theDiv.position().top;

//鼠標(biāo)元素相對(duì)偏移量

range.x = event.pageX - theDiv.position().left;

range.y = event.pageY - theDiv.position().top;

theDivId = theDiv.index();

theDivWidth = theDiv.width();

theDivHalf = theDivWidth / 2;

theDiv.removeClass("drag");

theDiv.addClass("drag-dash");

theDiv.css({left: initPos.x + 'px', top: initPos.y + 'px'});

//創(chuàng)建新元素插入拖拽元素之前的位置(虛線框)

$("

tempDiv = $(".dash");

$(".dash").css("width" , theDivWidth);

return false

});

});

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

if (!choose) {

return false;

}

if (!move) {

//恢復(fù)對(duì)象的初始樣式

theDiv.removeClass("drag-dash");

theDiv.addClass("drag");

tempDiv.remove(); //刪除新建的虛線div

choose = false;

return false;

}

theDiv.insertBefore(tempDiv); //拖拽元素插入到虛線div的位置上

//恢復(fù)對(duì)象的初始樣式

theDiv.removeClass("drag-dash");

theDiv.addClass("drag");

tempDiv.remove(); //刪除新建的虛線div

move = false;

choose = false;

return false

}).mousemove(function (event) {

if (!choose) {return false}

move = true;

lastPos.x = event.pageX - range.x;

lastPos.y = event.pageY - range.y;

lastPos.x1 = lastPos.x + theDivWidth;

//拖拽元素隨鼠標(biāo)移動(dòng)

theDiv.css({left: lastPos.x + 'px', top: lastPos.y + 'px'});

//拖拽元素隨鼠標(biāo)移動(dòng)查找插入目標(biāo)元素

var $main = $('.drag'); //局部變量:按照重新排列過的順序再次獲取各個(gè)元素的坐標(biāo),

$main.each(function () {

tarDiv = $(this);

tarPos.x = tarDiv.position().left;

tarPos.y = tarDiv.position().top;

tarPos.x1 = tarPos.x + tarDiv.width() / 2;

tarFirst = $main.eq(0); //獲得第一個(gè)元素\

tarFirstX = tarFirst.position().left + theDivHalf; //第一個(gè)元素對(duì)象的中心縱坐標(biāo)

//拖拽對(duì)象移動(dòng)到第一個(gè)位置

if (lastPos.x <= tarFirstX) {

tempDiv.insertBefore(tarFirst);

}

//判斷要插入目標(biāo)元素的坐標(biāo)后,直接插入

if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1) {

tempDiv.insertAfter(tarDiv);

}

});

return false

});

});

總結(jié)

以上是生活随笔為你收集整理的html 拖拽坐标,Html+css实现拖拽导航条的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。