完成效果:
只能在手机上演示效果,电脑上可以用chrome浏览器开发者工具中的UA来模拟手机。
<pre name="code" class="html"><!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" charset="utf-8">
<title>JS Bin</title>
<style>
* {
padding: 0;
margin: 0;
}
.nav {
width: 300px;
height: 200px;
overflow: hidden;
padding: 10px;
background: #eee;
border: 1px solid #E29E21;
position: absolute;
margin-top: 100px;
left: 50px;
}
ul {
list-style: none;
}
.head {
position: relative;
width: 3000px;
background: #ccc;
left: 0;
}
.box {
position: relative;
width: 50px;
height: 50px;
line-height: 50px;
background: green;
text-align: center;
float: left;
margin-left: 10px;
border-radius: 4px;
}
.selected {
background: #E29E21 !important;
color: #FFFFFF !important;
}
</style>
<script>
<span style="color:#ff0000;">$(document).ready(function() {
tag_num = $(".head >li").length;
nav_widthx = document.body.clientWidth; //获取可视区域宽度
max_width = (tag_num * 60 - nav_widthx + 100) * -1; //获取左滑最大距离
//alert(max_width);
document.getElementById("head").addEventListener('touchstart', touchStart);
document.getElementById("head").addEventListener('touchmove', touchMove);
document.getElementById("head").addEventListener('touchend', function() {
isMove = false;
});
});
//滑动开始事件
function touchStart(e) {
isMove = true;
e.preventDefault();
tx = parseInt($("#head").css('left'));
x = e.touches[0].pageX;
}
function touchMove(e) {
if (isMove) {
e.preventDefault();
var n = tx + e.touches[0].pageX - x;
if (n <= 0 && n > max_width) {
$("#head").css('left', n + 'px');
}
}
}</span>
</script>
</head>
<body>
<div class="nav">
<h1>在手机上实现图标滑动效果</h1>
<ul class="head" id="head">
<li class="box selected">标题一</li>
<li class="box">标题二</li>
<li class="box">标题三</li>
<li class="box">标题4</li>
<li class="box">标题5</li>
<li class="box">标题6</li>
<li class="box">标题7</li>
<li class="box">标题8</li>
<li class="box">标题9</li>
<li class="box">标题10</li>
<li class="box">标题11</li>
<li class="box">标题12</li>
</ul>
<br>
</div>
</body>
</html>
参考博客
背景:手机浏览器不支持JQuery的mousedown,mousemove,mouseup事件,想用jquery来写,但是百度过后发现jquery也没有支持手机手势滑动的方法。最终找到了上面的这篇博客才解决了问题。
<span style="color:#ff0000;">nav_widthx = document.body.clientWidth; //获取可视区域宽度</span>
<span style="color: rgb(255, 0, 0); font-family: Arial, Helvetica, sans-serif;">max_width = (tag_num * 60 - nav_widthx + 100) * -1; //获取左滑最大距离</span>
上述代码是为了在滑到最右边的时候,控制左滑最大距离,不会让菜单消失。
不足之处:
<span style="color:#ff0000;">document.getElementById("head").addEventListener('touchstart', touchStart);
document.getElementById("head").addEventListener('touchmove', touchMove);
document.getElementById("head").addEventListener('touchend', function() {
isMove = false;
});</span>
<span style="color:#ff0000;"></span><pre name="code" class="html" style="font-size:18px;"><span style="color:#ff0000;">tx = parseInt($("#head").css('left'));//这个地方也需要改</span>
元素绑定事件有些繁琐,不知道怎样写可以让调用的时候尽量简化。因为页面中不止这一个要滑动的部分。
最后像这样调用:$(".head").drag();
有知道怎么写的一定要告诉我啊~~