Div拖动不作为背景

时间:2012-10-31 12:37:28

标签: javascript jquery html css

我正在尝试拖动框,这会改变复选框的状态,僵尸程序不是我现在想要做的成就,我试图达到我的div不会拖动作为背景图像。 Div必须不是背景图像,可以添加背景图像。

(function($){
$.fn.slideCheckbox = function(){

    return this.each(function(){

        //init, native js for better performance
        slidesHolder = document.createElement( 'div' );
        slidesHolder.setAttribute( 'class', 'slides_holder' );
        slides = document.createElement( 'div' );
        slides.setAttribute( 'class', 'slides' );
        slidesHolder.appendChild( slides );

        slide_on = document.createElement( 'div' );
        slide_on.setAttribute( 'class', 'slide_on' );
        slide_off = document.createElement( 'div' );
        slide_off.setAttribute( 'class', 'slide_off' );
        slides.appendChild( slide_on );
        slides.appendChild( slide_off );
        this.parentNode.insertBefore( slidesHolder, this.nextSibling );

        // mouse hold and position
        var down = false;
        var clickPos = null;

        $( slidesHolder ).bind({

            mousedown : function( e ){

                clickPos = e.pageX;
                down = true;
            },
            mousemove : function( e ){
                if ( !down ) return;

                // slinkimas i desine
                if ( clickPos < e.pageX ) {

                    $( slides ).offset({ left: e.pageX - clickPos });
                }

                // slinkimas i kaire
                if ( clickPos > e.pageX ) {

                    $( slides ).offset({ left: e.pageX - clickPos  });
                }

            },
            mouseup : function(){

                down = false;
            }
        });
    });
}
 })(jQuery);

$(function(){

     $( '#slide_checkbox' ).slideCheckbox();
});

以下完整示例:

http://jsfiddle.net/GomatoX/w2DBU/

1 个答案:

答案 0 :(得分:0)

如果您已经在使用jquery,为什么不使用这样的东西?

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Slider - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(document).ready(function() {
    $( "#slider" ).slider({ max: 1 });
    $( "#slider" ).bind( "slidechange", function(event, ui) {
    document.getElementById('slide_checkbox').checked=$(this).slider("option","value");
    });

});
</script>
</head>
<body>
<label for="slide_checkbox">Slider Checkbox</labeL>
    <input type="checkbox" value="" name="" id="slide_checkbox" />
<div id="slider"></div>
</body>
</html>​

http://jsfiddle.net/q3hBj/