Home Web Front-end H5 Tutorial 实例帮助你了解HTML5滑动区域选择元素Slider element

实例帮助你了解HTML5滑动区域选择元素Slider element

Mar 24, 2017 am 10:40 AM
element html5

       HTML5的出现带给我们了很多新的标签和元素。其中一个就是区间选择输入元素,例如,选择10以内的一个数字。这个元素其实在很多系统操作系统中都存在了很长时间,但是现在只是如何将他们整合到浏览器中。
       因为使用JS可以很方便的模拟出这个效果所以HTML中一直没有可以直接使用的滑动选择元素。jQuery UI类库包含了一个非常不错的版本可以很容易进行样式设置。但是整合到浏览器中将非常简单,支持对于支持它的浏览器来说。

959.jpg

       浏览器支持
       除了著名的Firefox外所有的现代浏览器都支持这个元素,但是很容易使用html5slider.js来创建。当然IE也不支持区域选择输入,这个修改不太容易。这样的话,意味着你需要使用分开的类库类似jQuery UI来支持多浏览器。好消息在于如果浏览器不支持区域选择的话,它会做为一个输入框显示。

960.jpg

       如何工作的?
       区域选择输入元素使用输入框类似的标签,支持一般的数值属性,及其min和max,用来限制区域,step用来设置滑动中数值增量。缺省为1。

961.jpg

       你可以使用JS/jQuery来修改这些属性,也可以使用onchange事件来监听变化。代码如下:

<input id="defaultSlider" type="range" min="0" max="500" />
<p class="note">Current value: <span id="currentValue">0</span></p>
Copy after login

或者

$(function(){
        var currentValue = $(&#39;#currentValue&#39;);
        $(&#39;#defaultSlider&#39;).change(function(){
            currentValue.html(this.value);
        });
        // Trigger the event on load, so
        // the value field is populated:
        $(&#39;#defaultSlider&#39;).change();
});
Copy after login

当然这些代码需要浏览器支持。否则你只能看到一个输入框。
当然2/3的浏览器都看不到我们这个区域选择输入,我们需要想想别的方法。我们先快速使用jQueryUI来实现一个滑动选择器。

<p id="slider"></p>
<p class="note">Current value: <span id="currentValue">0</span></p>
Copy after login

你可以看到代码如下:

$(function(){
        var currentValue = $(&#39;#currentValue&#39;);
        $("#slider").slider({
                max: 500,
                min: 0,
                slide: function(event, ui) {
                        currentValue.html(ui.value);
                }
        });
});
Copy after login
  1. 代码非常简单。使用slider方法来实现。

  2. 最有意思的部分

  3. 因为我们已经实现了自己的区域选择方法,大家可以参考演示

  4. slider-knob.html

<p id="container">
        <p id="control"></p>
</p>

<!-- The range input is hidden and updated on each rotation of the knob -->
<input type="range" id="slider" min="0" max="500" value="25" />

<p class="note">Current value: <span id="currentValue">0</span></p>
Copy after login

assets/js/slider-knob.js

$(function(){
        var slider = $(&#39;#slider&#39;),
                min = slider.attr(&#39;min&#39;),
                max = slider.attr(&#39;max&#39;),
                currentValue = $(&#39;#currentValue&#39;);
        // Hiding the slider:
        slider.hide();
        $(&#39;#control&#39;).knobKnob({
                snap : 10,
                value: 250,
                turn : function(ratio){
                        // Changing the value of the hidden slider
                        slider.val(Math.round(ratio*(max-min) + min));
                        // Updating the current value text
                        currentValue.html(slider.val());
                }
        });
});
Copy after login

       以上代码使用min和max来计算数值。
       总结
       滑动选择对于用户使用来说比输入框非常方便 。虽然浏览器支持有限,但是你可以使用jQuery来增强相关功能。

以上就是实例帮助你了解HTML5滑动区域选择元素Slider element的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关文章:

hwSlider-内容滑动切换效果(一)

hwSlider-内容滑动切换效果(二):响应式可触控滑动

微信小程序 slider组件详细介绍

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles