javascript - 如何使这个 slider 控件双向工作?

标签 javascript jquery html css

我正在尝试制作一个范围 slider ,但它仅在单个方向(向右)工作并拖出父容器(#volume)。我该如何解决这个问题?

我附上了一个演示 fiddle 链接。

标记

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

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

$('.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/

最佳答案

您没有考虑为 body 元素提供的填充。 我对代码做了一些修改。

  $('.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());
    });

 });

检查这个 jsFiddle http://jsfiddle.net/2mYm7/1/

关于javascript - 如何使这个 slider 控件双向工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23490761/

相关文章:

javascript - 如何从浏览器发送 http 删除请求?

javascript - 如何将MVC架构应用到ExtJS项目中?

javascript - 按照 firebase 文档,.update 仍在覆盖

c# - 字符串未定义

javascript - 检测图像何时加载

javascript - Colorbox的tabindex =“-1”属性可防止在表单字段上进行焦点和键入

php - Google checkout,如何清除我的购物车?

javascript - 将变量插入innerHTML

jquery - Firefox 中奇怪的 Bootstrap 进度条行为

php - 更改当天 php css 的背景颜色