Firefox范围滑块将拇指移到轨道上方

如何解决Firefox范围滑块将拇指移到轨道上方

我有一个自定义样式的范围滑块,该滑块在Chrome上效果很好,但是在Firefox上无法正常工作。

我面临的主要问题是,在Chrome上,我能够将滑块的margin-top设置为负值,从而将其向上移动到轨道上方,如下图所示:

desired design

但是,在Firefox上,尽管自定义了滑块,但我无法执行此操作。我在我的代码上附加了一个小提琴:

$( window ).on( "load",function() { 
    sliderValues();    
});

$( window ).resize(function() {
   sliderValues();  
});

let mouseDown;
$(document).mousedown(function() {
    mouseDown = true;
}).mouseup(function() {
    mouseDown = false;  
});


let stepChangeThreshold = 3000;


$('#range').on('mousemove',function(e){
    if (!mouseDown) return;
    
    this.prevIoUsClientX = this.prevIoUsClientX || e.clientX;
    let stepChangeThresholdPositionX = this.getBoundingClientRect().x+($('#range').width()*stepChangeThreshold/5500);
    
    
    if (this.value == stepChangeThreshold){
        if ((this.prevIoUsClientX > e.clientX) && (e.clientX < stepChangeThresholdPositionX)){
            this.step = 250;
        } else {
            this.step = 500;
        }
        
        
    }


    this.prevIoUsClientX = e.clientX;
});


$('#range').on('input',function() {    
  sliderValues();   

  var val = $('#range').val();  
  if (val >= stepChangeThreshold) {
      this.step = 500;
  } else {
      this.step = 250;
  };
  this.prevIoUsVal = val;
    
});

function sliderValues(){
    var val = $('#range').val();
    var min = $('#range').attr('min');
    var max = $('#range').attr('max');
    var portion = (val - min) / (max - min);
    $('#rangeV').html('<span>$'+ val +'</span>');
    $('#rangeV').css('left',portion * ($('#range').width() - 70));
    
    let thumbSliderRatio = (70/$('#range').width())*100;
    let fillPercent = Number(portion*(100-thumbSliderRatio) + thumbSliderRatio/2) + "%";
    
    $('#range').css('background','linear-gradient(to right,#008e39 0%,#008e39 ' + fillPercent + ',#ccc ' + fillPercent + ',#ccc 100%)');     
};
.borrowheading {
    text-align: center;
    font-size: 21px;
    font-family: 'Open Sans',sans-serif;
    font-weight: 800;
    padding: 25px;
    margin-top: 50px;
}

#range {
    -webkit-appearance: none;
    margin: 20px 0;
    width: 100%;
    background: linear-gradient(to right,#008e39 50%,#ccc 50%,#ccc 100%);
    border-radius: 8px;
    height: 7px;
    width: 100%;
    outline: none;
    
}
#range:focus {
    outline: none;
}

#range::-webkit-slider-thumb {
    height: 70px;
    width: 70px;
    border-radius: 50%;
    background-color: #008e39;
    cursor: pointer;
    -webkit-appearance: none;
    margin-top: -100px;
}

#range::-moz-range-thumb {
    height: 70px;
    width: 70px;
    border-radius: 50%;
    background-color: #008e39;
    cursor: pointer;
    margin-top: -100px;
    border: none;
}

.range-wrap{
    width: 90%;
    position: relative;
    margin: auto;
    margin-top: 120px;
}
.range-value{
    position: absolute;
}
.range-value span{
    width: 70px;
    height: 70px;
    text-align: center;
    color: #fff;
    font-size: 20px;
    line-height: 48px;
    display: block;
    position: absolute;
    pointer-events: none;
    font-family: 'Open Sans',sans-serif;
    font-weight: 700;
    margin-top: -64px;
}

.range-value span:after {
    content: "";
    position: absolute;
    width: 0;
    height: 36px;
    border: 2px solid #008e39;
    top: 55px;
    left: calc(50% - 1px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h4 class="borrowheading">How much would you like to borrow today?</h4>
<div class="range-wrap">
    <div class="range-value" id="rangeV"></div>
    <input id="range" type="range" min="500" max="5000" step="250" value="1000">
</div>

如何在Firefox上将滑块增加100像素?

解决方法

可以使用transform而不是使用负边距,并将range元素移向y负轴。另外,我给包含价格的span设置了z-index: 1,并减小了伪元素的高度,该伪元素表示可拖动范围元素和滑块本身之间的连接条。

$( window ).on( "load",function() { 
    sliderValues();    
});

$( window ).resize(function() {
   sliderValues();  
});

let mouseDown;
$(document).mousedown(function() {
    mouseDown = true;
}).mouseup(function() {
    mouseDown = false;  
});


let stepChangeThreshold = 3000;


$('#range').on('mousemove',function(e){
    if (!mouseDown) return;
    
    this.previousClientX = this.previousClientX || e.clientX;
    let stepChangeThresholdPositionX = this.getBoundingClientRect().x+($('#range').width()*stepChangeThreshold/5500);
    
    
    if (this.value == stepChangeThreshold){
        if ((this.previousClientX > e.clientX) && (e.clientX < stepChangeThresholdPositionX)){
            this.step = 250;
        } else {
            this.step = 500;
        }
        
        
    }


    this.previousClientX = e.clientX;
});


$('#range').on('input',function() {    
  sliderValues();   

  var val = $('#range').val();  
  if (val >= stepChangeThreshold) {
      this.step = 500;
  } else {
      this.step = 250;
  };
  this.previousVal = val;
    
});

function sliderValues(){
    var val = $('#range').val();
    var min = $('#range').attr('min');
    var max = $('#range').attr('max');
    var portion = (val - min) / (max - min);
    $('#rangeV').html('<span>$'+ val +'</span>');
    $('#rangeV').css('left',portion * ($('#range').width() - 70));
    
    let thumbSliderRatio = (70/$('#range').width())*100;
    let fillPercent = Number(portion*(100-thumbSliderRatio) + thumbSliderRatio/2) + "%";
    
    $('#range').css('background','linear-gradient(to right,#008e39 0%,#008e39 ' + fillPercent + ',#ccc ' + fillPercent + ',#ccc 100%)');     
};
.borrowheading {
    text-align: center;
    font-size: 21px;
    font-family: 'Open Sans',sans-serif;
    font-weight: 800;
    padding: 25px;
    margin-top: 50px;
}

#range {
    -webkit-appearance: none;
    margin: 20px 0;
    width: 100%;
    background: linear-gradient(to right,#008e39 50%,#ccc 50%,#ccc 100%);
    border-radius: 8px;
    height: 7px;
    width: 100%;
    outline: none;
    
}
#range:focus {
    outline: none;
}

#range::-webkit-slider-thumb {
    height: 70px;
    width: 70px;
    border-radius: 50%;
    background-color: #008e39;
    cursor: pointer;
    -webkit-appearance: none;
    transform: translateY(-65px);    
}

#range::-moz-range-thumb {
    height: 70px;
    width: 70px;
    border-radius: 50%;
    background-color: #008e39;
    cursor: pointer;
    border: none;
    transform: translateY(-65px);  
}

.range-wrap{
    width: 90%;
    position: relative;
    margin: auto;
    margin-top: 120px;
}
.range-value{
    position: absolute;
}
.range-value span{
    width: 70px;
    height: 70px;
    text-align: center;
    color: #fff;
    font-size: 20px;
    line-height: 48px;
    display: block;
    position: absolute;
    pointer-events: none;
    font-family: 'Open Sans',sans-serif;
    font-weight: 700;
    margin-top: -64px;
    z-index: 1;
}

.range-value span:after {
    content: "";
    position: absolute;
    width: 0;
    height: 32px;
    border: 2px solid #008e39;
    top: 55px;
    left: calc(50% - 1px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h4 class="borrowheading">How much would you like to borrow today?</h4>
<div class="range-wrap">
    <div class="range-value" id="rangeV"></div>
    <input id="range" type="range" min="500" max="5000" step="250" value="1000">
</div>

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?