简体   繁体   English

范围 slider html css javascript

[英]range slider html css javascript

I'm planning to make a 'range slider' type in registration form and the JavaScript validation 'must be 16 years and above'.我打算在注册表中制作一个“范围滑块”类型,并且 JavaScript 验证“必须年满 16 岁”。

Example if I'm plan make a JavaScript valuation "must be minimum 0 years and maximum 50 years", these are the html例如,如果我计划进行 JavaScript 估值“必须最短 0 年,最长 50 年”,这些是 html

<input type="range" id="vol" name="vol" min="0" max="50">

But if I'm assuming 'must be 16 years and above' the minimum is 16 years old and the maximum is infinite, what would the html code be?但是,如果我假设“必须年满 16 岁”,最小值为 16 岁,最大值为无限,那么 html 代码是什么? is infinite possible or the maximum is always 100?是无限可能还是最大值总是 100?

Normally I would point to the MDN reference in a question, but here I would say what it provides is somewhat contradictory.通常我会在问题中指向 MDN 参考,但在这里我会说它提供的内容有些矛盾。 In the max attribute section of the range <input/> MDN reference they state:范围<input/>max属性部分 MDN 参考他们 state:

The greatest value in the range of permitted values.允许值范围内的最大值。 If the value entered into the element exceeds this, the element fails constraint validation.如果输入到元素中的值超过此值,则该元素将无法通过约束验证。 If the value of the max attribute isn't a number, then the element has no maximum value.如果 max 属性的值不是数字,则该元素没有最大值。

This would make it sound as though an infinite upper range is a possibility.听起来好像无限上限是可能的。 However, the MDN reference also states that default max value is 100 .但是,MDN 参考还指出默认max100 Testing in the dev tools, omitting the max does indeed seem to show that, if omitted, the <input type="range" /> will simply default the maximum to 100.在开发工具中测试,省略max似乎确实表明,如果省略, <input type="range" />将简单地将最大值默认为 100。

If we think about this more, it makes sense.如果我们更多地考虑这一点,这是有道理的。 Unlike a <input type="number" /> , which simply shows the numerical value as a number, a <input type="range" /> is a graphical, geometric representation of the number.<input type="number" />将数值简单地显示为数字不同, <input type="range" />是数字的图形几何表示 Given this, how could we convey an infinite upper bound without an infinitely long screen and slider?鉴于此,我们如何在没有无限长屏幕和 slider 的情况下传达无限上限? If you set the slider to be a finite width and somehow made it so that the far right edge represented infinity, how would you determine where to stop incrementing numbers and change to infinity?如果您将 slider 设置为有限宽度并以某种方式使其最右边缘代表无穷大,您将如何确定停止递增数字并更改为无穷大的位置?

(I will state that, technically, in JS typeof Infinity is a number. However, putting that in the max attribute of a <input type="range" /> just defaults it to 100 .) (我state 从技术上讲,在 JS typeof Infinity中是一个数字。但是,将它放在<input type="range" />max属性中只是将其默认为100 。)

信贷范围滑块 giphy animated gif link giphy动画gif链接

I have developed a range slider but it uses html css and angular (typescript).我开发了一个范围 slider 但它使用 html css 和 angular(打字稿)。 You may have to modify code slightly but it works by using a CSS circle, placing the number value at a position in that circle, and placing that over the input range.您可能需要稍微修改代码,但它的工作原理是使用 CSS 圆圈,将数字值放在该圆圈中的 position 处,并将其放在输入范围内。

html section html款

<div class="row small-margin"><div class="col-lg-8 small-padding">
<div class="form-group has-success">
  <label>Credit Score</label>
  <br />
  <div class="ui medium-padding">
    <div class="slidecontainer">
      <input #creditSlider (input)="updateCreditSlider()"  (change)="updateCreditSlider()" type="range" min="300" max="850" value="{{ creditSliderValue }}" class="slider" id="myRange">
    </div>
  </div>
  <div #creditSliderSpan class="sliderValue circle" >
    <span class="noselect">{{ creditSliderValue }}</span>
  </div>
  <div #creditSliderTrack class="sliderTrack" ></div>
</div>

typescript section (In the angular component class) typescript部分(在angular组件类中)

creditSliderValue : any;
@ViewChild('creditSlider') creditSlider;
@ViewChild('creditSliderSpan') creditSliderSpan;
.
.
.
updateCreditSlider() {
      let horizontalOffset:number = 0;
      //values from 300 to 850 - Next value needs to be adjusted based on your placement of slider object
      horizontalOffset =  ( (Number.parseInt(this.creditSlider.nativeElement.value )- 280)/2.45);
      this.creditSliderSpan.nativeElement.style.left = ( horizontalOffset )+ 'px';
      this.creditSliderSpan.nativeElement.style.top = this.creditSliderSpan.nativeElement.style.top  + 'px';
      this.creditSliderValue = this.creditSlider.nativeElement.value;
    }

css section css款

.smallPadding {
  margin-bottom: 0px;
  padding: 4px;
}

.slidecontainer {
    width: 100%; /* Width of the outside container */
}

/* The slider itself */
.slider {
    position: relative;
    -webkit-appearance: none;  /* Override default CSS styles */
    appearance: none;
    width:275px;
    //width: 100%; /* Full-width */
    height: 15px;
    border-radius: 5px;
    //background: #d3d3d3; /* Grey background */
    background: rgba(211,211,211, 0.00);

    outline: none; /* Remove outline */
    opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
    -webkit-transition: .2s; /* 0.2 seconds transition on hover */
    transition: opacity .2s;
    z-index: 20;
}

/* Mouse-over effects */
.slider:hover {
    //opacity: 1; /* Fully shown on mouse-over */
}

/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */
.slider::-webkit-slider-thumb {
    position: relative;
    -webkit-appearance: none; /* Override default look */
    appearance: none;
    width: 50px; /* Set a specific slider handle width */
    height: 50px;
    border-radius: 50%;
    border-style: none;
    //background: #4CAF50; /* Green background */
    background: rgba(76,175,80, 0.00);
    cursor: pointer; /* Cursor on hover */

}

.slider::-moz-range-thumb {
    width: 25px; /* Set a specific slider handle width */
    height: 25px;
    border-radius: 50%;
    border-style: none;
    //background: #4CAF50; /* Green background */
    background: rgba(76,175,80, 0.00);
    cursor: pointer; /* Cursor on hover */

}

.sliderValue {
    position: absolute;
    top: 25px;
    left: 0px;
    width: 100%; /* Width of the outside container */
    z-index: 15;

}
.noselect {
  -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
     -khtml-user-select: none; /* Konqueror HTML */
       -moz-user-select: none; /* Firefox */
        -ms-user-select: none; /* Internet Explorer/Edge */
            user-select: none; /* Non-prefixed version, currently
                                  supported by Chrome and Opera */
}

@media screen and (max-width: 992px) {

}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  font-size: 14pt;
  color: black;
  line-height: 46px;
  text-align: center;
  background: white;
  vertical-align: center;
  //display: table-cell;
  border-style: solid;
  border-color: #7E7E7E;
}

.medium-padding {
  padding-top: 14px;
  padding-bottom: 14px;
}
.sliderTrack {
  position: relative;
  width: 220px;
  height: 10px;
  background-color: #E0E1E2;
  vertical-align: center;
  border-radius: 5px;
  top: -35px;
  left: 25px;
  //display: table-cell;
  //border-style: solid;
  //border-color: #7E7E7E;
}
.left-padding {
  padding-left: 14px;

}

.small-margin {
  margin-left: 4px;
}
.medium-margin {
  margin-left: 10px;
}
.small-padding{
  padding: 4px;
}

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

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