jquery自定义控件拖拽框dragbox

概述:

在做项目的过程中遇到了拖拽框的使用,虽然网上有很多类似的插件,但总归不如自己的好使,于是就自己写了一个,在此总结下来,以便后用。

效果:

提供方法:

setTitle(title):设置标题;

setContent(content):设置内容;

setsize(width, height):设置大小;

hide():隐藏;

show():显示;

使用方法:

创建对象

            var dragbox = $("#dragbox").DragBox({
                title:"拖拽的框子",
                content:"拖拽的框子",
                width:200,
                height:100
            });

调用方法

dragbox.setSize(250,200);
var content = $("<h1 />").html("我是中国人!");
dragbox.setContent(content);
dragbox.show();

代码:

style.css

body,html,.dragbox{
    font-size: 12px;
}
.dragbox .close{
    float: right;
    margin: 10px 8px;
    width: 10px;
    height: 10px;
    background: url("img/iw_close.gif") no-repeat;
}
.dragbox .close:hover{
    cursor: pointer;
}

.dragbox .title{
    border: 1px solid #bbbbbb;
    padding:7px 10px;
    font-weight:bold ;
}

.dragbox .content{
    border: 1px solid #bbbbbb;
    border-top: none;
    padding: 7px 10px;
    box-shadow: 1px 1px 1px #dddddd;
}

jquery.custom.dragbox.js

(function($){
    $.fn.DragBox = function(options){
        var defaults = {
            title:"dragbox",
            content:"dragbox",
            width:200,
            height:100
        };
        var options = $.extend(defaults,options);
        var _title = options.title;
        var _content = options.content;
        var _width = options.width,  _height = options.height;

        var _dragBox = $(this);
        _dragBox.css("width",_width+"px").addClass("dragbox");
        var _close = $("<div />").appendTo(_dragBox).addClass("dragbox close");
        var _titleBox = $("<div />").appendTo(_dragBox).addClass("dragbox title").html(_title);
        var _contentBox = $("<div />").appendTo(_dragBox).addClass("dragbox content").css("height",_height+"px").append(_content);

        var _x,_y;
        var _flag = false;

        _titleBox.on("mousedown",function(e){
            _flag = true;
            _titleBox.css("cursor","move");
            _x= e.pageX - parseInt(_dragBox.css("left"));
            _y= e.pageY - parseInt(_dragBox.css("top"));
            var docWidth = $(document).width(),docHeight = $(document).height();
            var boxWidth = _dragBox.width(),boxHeight = _dragBox.height();
            $(document).on("mousemove",function(e){
                if(_flag){
                    var x = e.pageX-_x,y = e.pageY-_y;
                    _dragBox.css({"left":x+"px","top":y+"px"});
                }
            });
            $(document).on("mouseup",function(e){
                _flag = false;
                _titleBox.css("cursor","default");
            });
        })

        _close.on("click",function(){
            _dragBox.fadeOut();
        })

        function show(){
            _dragBox.fadeIn();
        }
        function hide(){
            _close.click();
        }
        function setTitle(title){
            _titleBox.html(title);
        }
        function setContent(content){
            _contentBox.html(content);
        }
        function setSize(width,height){
            _dragBox.css("width",width+"px");
            _contentBox.css("height",height+"px");
        }
        this.show = show;
        this.hide = hide;
        this.setTitle = setTitle;
        this.setContent = setContent;
        this.setSize = setSize;
        return this;
    }
})(jQuery);

调用代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    <style>
        #dragbox{
            position: absolute;
            top:50px;
            left: 50px;
        }
    </style>
    <script src="http://localhost/jquery/jquery-1.8.3.js"></script>
    <script src="js/jquery.custom.dragbox.js"></script>
    <script>
        $(function(){
            var dragbox = $("#dragbox").DragBox({
                title:"拖拽的框子",
                content:"拖拽的框子",
                width:200,
                height:100
            });

            $("#show").on("click",function(){
                dragbox.setSize(250,200);
                var content = $("<h1 />").html("我是中国人!");
                dragbox.setContent(content);
                dragbox.show();
            })
        });
    </script>
</head>
<body>
<button id="show">显示拖拽框</button>
<div id="dragbox"></div>
</body>
</html>

时间: 2024-08-01 10:43:57

jquery自定义控件拖拽框dragbox的相关文章

jQuery可拖拽3D万花筒旋转特效

jQuery可拖拽3D万花筒旋转特效 这是一个使用了CSS3立体效果的强大特效,本特效使用jQuery跟CSS3 transform来实现在用户鼠标按下拖动时,环形图片墙可以跟随鼠标进行3D旋转动画. 效果体验:http://hovertree.com/texiao/jquery/92/ 进去后可以上下左右的拖动图片. 本示例中使用到了CSS3的transform-style 属性,该规定如何在 3D 空间中呈现被嵌套的元素. 默认值: flat继承性: no版本: CSS3JavaScript

js实现拖拽框

纯js版的拖拽框,该效果很简单 直接看代码..... <!doctype html> <html> <head> <meta charset="urf-8"/> <style> #box{ border:1px solid #ccc; position:absolute; border-radius:4px; border-shadow:10px 10px 5px #888888; } #box h1{ margin:0; p

jquery实现拖拽的效果

上篇讲的是原生js实现拖拽的效果,本篇是jquery实现拖拽的效果. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8"/> <title> New Documen

SpringMvc+jQuery 文件拖拽上传、选择上传

最近做了个简易的文件上传功能,jsp版本的,后续会完善更多的功能,不过现在已经能用了,需要的小伙伴,直接引用下面的文件内容直接copy到自己的项目中就ok了,效果如图: fileupload.css: .fileupload_box { position:relative; width: 100%; height: 100%; border: 3px dashed #E5E5E5; text-align: center; z-index: 2000; cursor: pointer; margi

JQuery之拖拽插件

一直以来,都对JS获取元素的位置感到非常的困惑:一会client.一会offset.一会scroll. 再加上各大浏览器之间的不兼容,唉,搞得哥晕晕乎乎的. 而很多页面效果都要用到这些位置.不得已,得练练,得记记. 下面就来说说这个基于 JQuery的简易拖拽插件吧. 按惯例,先说说拖拽的原理,以及搞这么一个东东的步骤: 那什么是拖拽呢? 看名字就知道了:就是把一个东东拖来拽去的. 放到我们的DOM上,就是改变它的位置. 它只有两个难点:1.如何知道是在拖? 2.如何知道从哪拖,拖到哪? 其实,

自定义拖拽框插件

1.HTML代码如下 注意引入jquery和自定仪的drag.js , 路径正确打开页面就能看到效果 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus?"> <meta name="Author&

jQuery 鼠标拖拽排序

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>测试的拖拽功能</title> <style type="text/css"> body, div { margin: 0; paading: 0; f

jquery鼠标拖拽效果分享

//html代码部分 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="../js/jquery-1.11.1.js"></script> </head> <style> body

jquery gridster 拖拽功能 中文文档

先上git连接 http://git.oschina.net/alexgaoyh/Draging/blob/master/src/main/webapp/gridster/adding-widgets-dynamically.html 效果截图: 1)动态添加模块(http://gridster.net/demos/adding-widgets-dynamically.html):使用add_widget方法,通过数组创建一个gridster实例,产生的模块自动排列,无需指定相对位置. (2)自