English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementación de la función de desplazamiento horizontal en la barra de navegación en js

Efecto como se muestra a continuación:

Código como se muestra a continuación:

<!DOCTYPE HTML>
<html>
<head>
 <meta charset="UTF-8">
 <título>div deslizamiento horizontal ordenación</título>
 <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> 
 <style type="text/css">
  body, div {
   rejilla: 0px;
   margen: 0px;
  }
  .box {
   posición: relativa;
   margen-izquierda: 15px;
   relleno: 10px;
   rejilla-derecha: 0px;
   ancho: 810px;
   border: azul sólido 1px;
  }
  .box ul{
   lista-estilo: ninguno;
   desbordamiento: oculto;
   relleno: 0;
   margen:0;
  }
  .drag {
   float: left;
   border: #000 sólido 1px;
   texto-alineación: centro;
  }
  .box ul li a{
   display: bloque;
   relleno: 10px 25px;
  }
  .drag-dash {
   posición: absoluta;
   border: #000 sólido 1px;
   fondo: #ececec;
  }
  .dash {
   float: left;
   border: 1px sólido transparente;
  }
 </style>
</head>
<body>
<h1>div arrastrar y ordenar horizontalmente</h1>
<div class="box">
 <ul>
  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Navegación uno</a></li>
  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Navegación dos navegación</a></li>
  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Navegación navegación navegación tres</a></li>
  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Navegación navegación cuatro</a></li>
  <li class="drag"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow">导五</a></li>
 </ul>
</div>
<script type="text/javascript">
 $(document).ready(function () {
  var range = {x: 0, y: 0};//鼠标元素偏移量
  var lastPos = {x: 0, y: 0, x1: 0, y1: 0}; //拖拽对象的四个坐标
  var tarPos = {x: 0, y: 0, x1: 0, y1: 0}; //目标元素对象的坐标初始化
  var theDiv = null, move = false;
  var choose = false; //拖拽对象 拖拽状态 选中状态
  var theDivId = 0, theDivHeight = 0, theDivHalf = 0;
  var tarFirstY = 0; //拖拽对象的索引、高度、的初始化。
  var tarDiv = null, tarFirst, tempDiv; //要插入的目标元素的对象, 临时的虚线对象
  var initPos = {x: 0, y: 0};
  var theDivWidth;//拖拽对象的宽度
  $(".drag").each(function () {
   $(this).mousedown(function (event) {
    choose = true;
    //拖拽对象
    theDiv = $(this);
    //记录拖拽元素初始位置
    initPos.x = theDiv.position().left;
    initPos.y = theDiv.position().top;
    //La cantidad de desplazamiento relativa del elemento del ratón
    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 + , top: initPos.y + );
    // Crear un nuevo elemento e insertarlo en la posición anterior al elemento arrastrado (marco de línea punteada)
    $("<div class='dash'></div>").insertBefore(theDiv);
    tempDiv = $(".dash");
    $(".dash").css("width", theDivWidth);
    return false
   });
  });
  $("document").mouseup(function (event) {
   if (!choose) {
    return false;
   }
   if (!move) {
    //Restaurar el estilo inicial del objeto
    theDiv.removeClass("drag-dash");
    theDiv.addClass("drag");
    tempDiv.remove(); // Eliminar el div de línea punteada recién creado
    choose = false;
    return false;
   }
   theDiv.insertBefore(tempDiv); // Insertar el elemento arrastrado en la posición del div de línea punteada
   //Restaurar el estilo inicial del objeto
   theDiv.removeClass("drag-dash");
   theDiv.addClass("drag");
   tempDiv.remove(); // Eliminar el div de línea punteada recién creado
   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;
   // Arrastrar el elemento mientras se mueve el ratón
   theDiv.css({left: lastPos.x + , top: lastPos.y + );
   // Arrastrar el elemento mientras se mueve el ratón para encontrar el elemento de destino para insertar
   var $main = $('.drag'); // Variable local: obtener nuevamente las coordenadas de cada elemento según el orden reordenado,
   $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); // Obtener el primer elemento\
    tarFirstX = tarFirst.position().left + theDivHalf; // Coordenada vertical del centro del primer elemento
    //Mover el objeto de arrastre al primer lugar
    if (lastPos.x <= tarFirstX) {
     tempDiv.insertBefore(tarFirst);
    }
    //Determinar la posición del elemento objetivo para insertar y agregar directamente
    if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1) {
     tempDiv.insertAfter(tarDiv);
    }
   });
   return false
  });
 });
</script>
</body>
</html>

Esto es todo el contenido de este artículo, espero que el contenido de este artículo pueda proporcionar cierta ayuda a su aprendizaje o trabajo, y también espero que apoyen más a la教程 ¡!

Declaración: El contenido de este artículo se obtiene de la red, es propiedad del autor original, el contenido se contribuye y carga de manera autónoma por los usuarios de Internet, este sitio no posee los derechos de propiedad, no se ha editado de manera humana y no asume ninguna responsabilidad legal. Si encuentra contenido sospechoso de infracción de derechos de autor, por favor envíe un correo electrónico a: notice#oldtoolbag.com (al enviar un correo electrónico, por favor reemplace # con @ para denunciar, y proporcione evidencia relevante. Una vez verificada, este sitio eliminará inmediatamente el contenido sospechoso de infracción.

Te gustará