用于重定向不适用于触摸设备-6ren"> 用于重定向不适用于触摸设备-我正在尝试制作一个 slider ,它在达到特定值时重定向到另一个 url。密码是http://jsfiddle.net/9vs2n/ HTML:- CSS:- body { co-6ren">
gpt4 book ai didi

javascript - 样式 <input type ="range"> 用于重定向不适用于触摸设备

转载 作者:太空宇宙 更新时间:2023-11-04 04:17:10 26 4
gpt4 key购买 nike

我正在尝试制作一个 slider ,它在达到特定值时重定向到另一个 url。密码是http://jsfiddle.net/9vs2n/
HTML:-

<div id="slide-wrap">
<div id="over">
<div id="overin"></div>
</div>
<input type="range" id="in" class="slideToUnlock" value="0" max="100">
</div>


CSS:-

body {
color:white;
background: #323232;
margin:0;
padding:0;
}
#over {
width:80%;
border:solid thick rgba(255, 255, 255, 0.5);
margin:0 auto;
padding:0;
height:55px;
display:block;
position:relative;
background:rgba(255, 255, 255, 0.75);
border-bottom-left-radius:1em;
border-top-right-radius:1em;
}
#overin {
appearance:none;
-webkit-appearance:none;
-moz-appearance:none;
background:#60a917;
width:75px;
height:55px;
display:block;
border-bottom-left-radius:1em;
border-top-right-radius:1em;
}
#in {
outline:none;
-moz-appearance:none;
-webkit-appearance:none;
appearance:none;
width:80%;
margin:0 auto;
margin-top:-55px;
padding:0;
height:55px;
display:block;
position:relative;
background:rgba(0, 0, 0, 0);
}
#overin:before {
color: white;
position:relative;
right:10px;
top: -10px;
width:99%;
font-size: 56px;
font-weight: bold;
content:">";
display:inline-block;
text-align:end;
}
#in::-webkit-slider-thumb {
cursor:pointer;
appearance:none;
-webkit-appearance:none;
-moz-appearance:none;
background:rgba(0, 0, 0, 0);
width:75px;
height:55px;
display:block;
}
#over:before {
content:"slide to continue";
color: white;
position: absolute;
left: 100px;
top: 10px;
z-index: -1;
font-size: 32px;
}
#slide-wrap {
margin:10px auto;
}


JavaScript:-

document.getElementById('in').onmouseup = function () {
var val = document.getElementById('in').value;
if (val >= 50) {

window.location.href = "http://www.example.com/";
} else {
this.value = 0;
zero()
}
}

document.getElementById('in').onmousemove = function () {
enlarge();
}

function enlarge() {
if (document.getElementById('in').value > 10) {
document.getElementById('overin').style.width = document.getElementById('in').value + "%";
}
}

function zero() {
document.getElementById('overin').style.width = '80px';
}

setInterval(loop1(), 1);

function loop1() {
document.getElementById('over').style.width = document.getElementById('in').style.width;
}

可以正常使用,但问题是它不能在触摸设备上使用,我在ipad上试过了,请尝试解决这个问题。该页面位于 https://dl.dropboxusercontent.com/u/99185097/donotdelete/index.html

最佳答案

简单。您必须为此添加一些触摸事件。 onmouseup & onmousestart 只会触发鼠标事件,据我所知,在触摸屏上不起作用。您必须添加的事件还有 touchmovetouchendHere's a reference link from MDN for touch events of HTML5.

这就是您的代码需要的结构:

var el = document.getElementById('in');

//mouse events
//gave names to the functions bound this, since w'll be using them two times.
el.onmousemove = enlarge;
el.onmouseup = redirect;


//touch events
//check if device has touch screen
// CREDIT : http://stackoverflow.com/a/4819886/1217785
if (is_touch_device()) {
console.log(true);
//onmousemove = touchmove
el.addEventListener("touchmove", enlarge, false)
//onmouseup = touchend
el.addEventListener("touchend", redirect, false)
}

//this function checks whether the device is a touch screen or not, else no binding will be done.
function is_touch_device() {
var isTouch = 'ontouchstart' in window ||
'onmsgesturechange' in window;

console.log(isTouch);
return isTouch;
};

//the rest of your functions

function redirect() {
var val = document.getElementById('in').value;
if (val >= 50) {

window.location.href = "http://www.example.com/";
} else {
this.value = 0;
zero()
}
}


function enlarge() {
if (document.getElementById('in').value > 10) {
document.getElementById('overin').style.width = document.getElementById('in').value + "%";
}
}

//other code

这是更新后的演示:http://jsfiddle.net/hungerpain/9vs2n/6/

希望这对您有所帮助!

关于javascript - 样式 &lt;input type ="range"> 用于重定向不适用于触摸设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19740612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com