Jquery DIV 挪动

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="title" content="Jquery Div自由拖动排序" />
<meta name="keywords" content="Jquery Div自由拖动排序,JqueryDiv排序,Jquery拖动,排序" />
<meta name="description" content="Jquer模块自由拖动排序-彭亚欧个人博客代码中心" />
<title>Jquery Div自由拖动排序</title>
<style type="text/css">
.cur {
    width: 200px;
    height: 90px;
    background-color: #0078ca;
    position: absolute;
    cursor: pointer;
    text-align: center;
    color: #fff;
    font: 35px "微软雅黑", Arial, Helvetica, sans-serif;
    padding-top: 30px;
}
#nblock_mid {
    position: absolute;
    width: 1000px;
    left: 30px;
}
.linkA {
    color: #333;
    font: 25px "微软雅黑", Arial, Helvetica, sans-serif;
    text-decoration:none;
}
</style>
</head>
<body>
<div id="nblock_mid">
  <div class="cur"></div>
  <div class="cur"></div>
  <div class="cur"></div>
  <div class="cur"></div>
  <div class="cur"></div>
  <div class="cur"></div>
  <div class="cur"></div>
  <div class="cur"></div>
  <div class="cur"></div>
</div>

</body>
<script type="text/javascript" src="http://www.pengyaou.com/jquery-1.4.min.js">
    </script>
<script type="text/javascript">
        $(document).ready(function(e) {
            SortDivHandler.Initialize();
            if (typeof(document.onselectstart) != "undefined") {
                // IE下禁止元素被选取
                document.onselectstart = new Function("return false");
            } else {
                // firefox下禁止元素被选取的变通办法
                document.onmousedown = new Function("return false");
                document.onmouseup = new Function("return true");
            }
        });
        var SortDivHandler = {
            CurrentLocationX: 0,
            CurrentLocationY: 0,
            CurrentSortFlag: 0,
            CurrentSortDiv: null,
            CurrentSortMove: 0,
            Initialize: function() {
                var isStart = false;
                var curCount = parseInt(($("#nblock_mid").width() - 90) / 300);
                for (var i = 0; i < $(".cur").length; i++) {
                    var floorCount = Math.ceil((i + 1) / curCount);
                    $($(".cur")[i]).attr("id", i);
                    $($(".cur")[i]).html(i + 1);
                    if ((i + 1) <= curCount) {
                        $($(".cur")[i]).animate({
                            left: 30 + i * 200 + (i - 1) * 30 + "px",
                            top: "30px"
                        }, 200 + i * 100);
                    } else if ((i + 1) > 3) {
                        var itemIndex = (i) % 3;
                        $($(".cur")[i]).animate({
                            left: 30 + itemIndex * 200 + (itemIndex - 1) * 30 + "px",
                            top: (floorCount - 1) * 120 + 30 * (floorCount - 1) + 30
                        }, 300 + i * 100);
                    }
                }
                var isDrag = true;
                $(".cur").mousedown(function(e) {
                    var SortTarget = $(this);
                    SortDivHandler.CurrentSortMove = 0;
                    SortDivHandler.CurrentSortDiv = $(this);
                    isDrag = true;
                    $(".cur").css("z-index", 1);
                    SortDivHandler.CurrentSortDiv.css("z-index", -1).css("opacity", 0.8);
                    SortDivHandler.CurrentLocationX = SortTarget.offset().left;
                    SortDivHandler.CurrentLocationY = SortTarget.offset().top;
                    SortTarget.attr("drag", 1);
                    SortTarget.css("position", "absolute");
                    SortTarget.css("cursor", "default");
                    var currentTarget = SortTarget;
                    var currentDisX = e.pageX - $(this).offset().left;
                    var currentDisY = e.pageY - $(this).offset().top;
                    $(document).mousemove(function(event) {
                        if ($(currentTarget).attr("drag") == 0 || SortDivHandler.CurrentSortMove == 1) return;
                        var currentX = event.clientX;
                        var currentY = event.clientY;
                        var cursorX = event.pageX - currentDisX; // $(this).offset().left;
                        var cursorY = event.pageY - currentDisY; //-$(this).offset().top;
                        // $(currentTarget)
                        $(currentTarget).css("top", cursorY - 8 + "px").css("left", cursorX - 30 + "px");
                        isStart = true;

                    });
                    $(document).mouseup(function() {
                        //  if(isDrag==false)return;
                        $(currentTarget).attr("drag", 0);

                    });
                });
                $(".cur").mousemove(function() {
                    if (isStart == false) return;
                    if (SortDivHandler.CurrentSortFlag == 0) {
                        if ($(this).attr("id") == SortDivHandler.CurrentSortDiv.attr("id")) {
                            return;
                        } else {
                            if (SortDivHandler.CurrentSortMove == 1) return;
                            SortDivHandler.CurrentSortMove = 1;
                            var targetX = $(this).offset().left;
                            var targetY = $(this).offset().top;
                            SortDivHandler.CurrentSortDiv.stop(true).animate({
                                left: targetX - 30 + "px",
                                top: targetY - 8 + "px"
                            }, 500, function() {
                                $(this).css("opacity", 1);
                            });
                            $(this).stop(true).animate({
                                left: SortDivHandler.CurrentLocationX - 30 + "px",
                                top: SortDivHandler.CurrentLocationY - 8 + "px"
                            }, 300, function() {});
                            isDrag = false;
                        }
                    }
                });
                $(".cur").mouseup(function() {
                    if (isDrag == false) return;
                    SortDivHandler.CurrentSortMove = 1;
                    SortDivHandler.CurrentSortDiv.stop(true).animate({
                        left: SortDivHandler.CurrentLocationX - 30 + "px",
                        top: SortDivHandler.CurrentLocationY - 8 + "px"
                    }, 500, function() {
                        SortDivHandler.CurrentSortDiv.css("z-index", -1).css("opacity", 1);
                    });
                });

            }
        }
    </script>
</html>

查看演示

转 http://www.pengyaou.com/codejq/JWJSKFSJK_46.html

时间: 2024-11-06 03:39:33

Jquery DIV 挪动的相关文章

Jquery div展开收缩

1 <html> 2 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 3 <head> 4 <script type="text/javascript" src="js/jquery1.3.2.js"></script> 5 <script type="tex

jquery div拖动效果示例代码

1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit

Jquery+div+css实现弹出登录窗口

基本思路先隐藏(dispaly:none)再显示,半透明蒙版层通过 z-index:9998; z-index:9999; 值越大越在前面 index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:

Jquery DIV滚动至浏览器顶部位置固定

获取元素(这里定位元素A)距离顶部的高度,接着设定scroll滚动的事件,比如超过那个高度,把A的位置设定为fixed,小于该高度,修改回relative. 方法一: $(function() { var elm = $('.nav'); var startPos = $(elm).offset().top; $.event.add(window, "scroll", function() { var p = $(window).scrollTop(); $(elm).css('pos

jquery &lt;div&gt; 排序

var asc_active = function(a, b) { var av = Number($(a).find('.active_num .v').html()); var bv = Number($(b).find('.active_num .v').html()); return av>bv ? 1 : -1;}var desc_active = function(a, b) { var av = Number($(a).find('.active_num .v').html());

jQuery DIV 隐藏与显示效果

 函数 描述 $(selector).hide() 隐藏被选元素 $(selector).show() 显示被选元素 $(selector).toggle() 切换(在隐藏与显示之间)被选元素 $(selector).slideDown() 向下滑动(显示)被选元素 $(selector).slideUp() 向上滑动(隐藏)被选元素 $(selector).slideToggle() 对被选元素切换向上滑动和向下滑动 $(selector).fadeIn() 淡入被选元素 $(selector

Jquery DIV滚动至浏览器顶部后固定不动代码

$(function(){ //获取要定位元素距离浏览器顶部的距离 var navH = $(".win").offset().top; //滚动条事件 $(window).scroll(function(){ //获取滚动条的滑动距离 var scroH = $(this).scrollTop(); //滚动条的滑动距离大于等于定位元素距离浏览器顶部的距离,就固定,反之就不固定 if(scroH>=navH){ $(".win").css({"po

JQuery之为某个div加入行样式

JQuery都是以$符号开头的.当然能够用jQuery取代$符号,他们是恒等的,同一时候也是相等的.()事实上就是一个方法,里面能够传递匿名函数等,选取某个div时,如id为div1则用$('#div1').记得加#号,这里跟我们写css样式时加的#号是相同的道理. 样例例如以下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <tit

jquery实现页面加载时删除特定class 的div内前三个字符

jQuery(document).ready(function(){        jQuery("div.groupheader").each(function(){ $(this).text($(this).text().substr(3));      });});