简体   繁体   English

如何使此滑块控件在两个方向上都能工作?

[英]How to make this slider control to work in both directions?

I'm trying to make a range slider but it's working in single direction(to right) only and dragging out of parent container(#volume). 我正在尝试制作范围滑块,但它仅在单个方向上(向右)工作,并拖出父容器(#volume)。 How can I fix this? 我怎样才能解决这个问题?

I've attached a demo fiddle link. 我已经附上了演示小提琴链接。

Markup 标记

<div id="volume">
    <div class="progress">
        <div class="volumeslider"></div>
    </div>
</div>

CSS 的CSS

#volume{
    width:300px;
    background: #ddd;
    cursor:pointer;
}
.progress{
    height:10px;
    background:#999;
    position:relative;
    width:0;
}
.volumeslider {
    background: #808080;
    position: absolute;
    width: 20px;
    height: 20px;
    border-radius: 15px;
    right: -10px;
    top: -5px;
}

JS JS

$('.volumeslider').bind('mousedown', function(e){
    $('.volumeslider').bind('mousemove', function(e){
        $('.progress').width(e.pageX - $('.progress').offset().left + 'px');
        $(this).css('left', e.pageX - ($(this).width()/2) );
    });
    $('.volumeslider').bind('mouseup',function(){
        $('.volumeslider').unbind('mousemove');
    });
});

JS Fiddle: http://jsfiddle.net/2mYm7/ JS小提琴: http : //jsfiddle.net/2mYm7/

You have not taken into consideration the padding you have given to the body element. 您尚未考虑为body元素添加的填充。 I have made some changes to the code. 我对代码做了一些更改。

  $('.volumeslider').bind('mousedown', function (e) {
      console.log('binded');
    $('.volumeslider').bind('mouseup', function (e) {
        console.log('unbinded');
       $('.volumeslider').unbind('mousemove');
    });
    $('.volumeslider').bind('mousemove', function (e) {
      console.log('mousemove');
      $('.progress').width(e.pageX -  $('.progress').offset().left + 'px');
      $(this).css('left', e.pageX - 25- $(this).width());
    });

 });

Check this jsFiddle http://jsfiddle.net/2mYm7/1/ 检查此jsFiddle http://jsfiddle.net/2mYm7/1/

Here's an example of how to make it work always and everywhere. 这是一个如何使其始终无处不在工作的示例。 Right now it will stay dragging forever if you leave the element. 现在,如果您离开元素,它将永远拖曳。

It includes border checks and makes sure the body is large enough so it stops dragging wherever on the page. 它包括边框检查,并确保主体足够大,因此无论页面上的任何位置都停止拖动。

http://jsfiddle.net/2mYm7/5/ http://jsfiddle.net/2mYm7/5/

var dragging = null;

function startDrag(){
    console.log('started dragging', this);
    var $this = $(this);
    dragging = $this;
    $(document.body).bind('mouseup', stopDrag);
    $(document.body).bind('mousemove', drag);
}

function stopDrag(){
    console.log('stopped dragging', dragging[0]);
    $(document.body).unbind('mouseup', stopDrag);
    $(document.body).unbind('mousemove', drag);
    dragging = null;
}

function drag(e){
    var slider = dragging;
    var progress = slider.parent();
    var container = progress.parent();
    var maxOffset = container.width();
    progress.width(Math.min(e.pageX - progress.offset().left, maxOffset) + 'px');
}

$('.volumeslider').bind('mousedown', startDrag);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM