前言:记得以前处理移动端横向滑动展示都是去用js去解决的,要用js进行蛮多处理,要算li的宽度,然后还要用js设置ul盒子的宽度,又要设置最大滑动距离,最小滑动距离等等.......但是现在发现用css就能很好的解决这功能。

一、直接上代码。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>  
<title>test</title>
<style>
body,p { margin:0; padding:0;}
.concent { margin:50px auto; width:100%; max-width:750px; min-width:320px; }
.box { white-space:nowrap; overflow-x:auto; }		/*注释1*/					
.box::-webkit-scrollbar { width:0; height:0; display: none; }     /*注释2*/
.box div { list-style:none; display:inline-block; width:100px; line-height:30px; margin-right:10px; 
background:#ccc; text-align:center; }	/*注释3*/
.box p { width:100%; height:50px; background:pink; }
.box div:last-child { margin:0; }
</style>
</head>
<body>
<div class="concent">
	<div class="box">	<!-- /*注释4*/ -->
	    <div>
            <p></p><b>简简单单</b></div><div>
            <p></p><b>简简单单</b></div><div>
            <p></p><b>简简单单</b></div><div>
            <p></p><b>简简单单</b></div><div>
            <p></p><b>简简单单</b></div><div>
            <p></p><b>简简单单</b></div>
	</div>
</div>
<script>
        var u = navigator.userAgent;
	var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
	var isUc = u.indexOf('UCBrowser') > -1;    //uc浏览器
	//var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	if(isAndroid&&isUc){		/*注释5*/
	    $('.box').on('touchstart',function(){
		$(document).on('touchmove',function(e){
		    e.preventDefault();
		});
		$(document).on('touchend',function(){
		    $(document).unbind();
		});
	    });
	}
</script>
</body>
</html>

nicescroll滑动到底部 滑动css_nicescroll滑动到底部

运行代码,发现就可以实现左右滑动了。

 

二、注释解析与注意事项。

注释①,注释③:改变li标签为行内块元素(inline-block),给.box添加一个white-space:nowrap; 不让他自动换行。再添加overflow-x:auto;让他超出部分滚动显示。

注释④ : 是为了解决行内块元素之间的默认间隙问题。

注释②: ::-webkit-scrollbar { width:0; height:0; display: none; } 是为了解决安卓浏览器的滚动条问题,在iphone浏览器上的常规浏览器上不会出现横向的滚动条,但是在安卓设备上的chrome,火狐浏览器等一些浏览器会出现滚动条,安卓设备上高版本的uc浏览器,qq自带浏览器,微信自带浏览器不会出现滚动条。(该方法无法解决火狐浏览器出现的滚动条,网上查找了蛮多资料,都建议js去解决这问题)。

注释⑤:是为解决安卓设备上的uc浏览器一个恶心的功能,就是安卓设备上的uc浏览器如果打开好几个窗口页面。向左向右滑的时候会跳转到其他页面,这时就需要取消默认事件。

 

三、(补充)通过scrollLeft,点击li标签进行位置滑动

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>  
<title>test</title>
<style>
body,p { margin:0; padding:0;}
.concent { margin:50px auto; width:100%; max-width:750px; min-width:320px; }
.box { white-space:nowrap; overflow-x:auto; }							
.box::-webkit-scrollbar { width:0; height:0; display: none; }     
.box div { list-style:none; display:inline-block; width:100px; line-height:30px; margin-right:10px; 
background:#ccc; text-align:center; }	
.box p { width:100%; height:50px; background:pink; }
.box div:last-child { margin:0; }
</style>
</head>
<body>
<div class="concent">
	<div class="box">	
	    <div>
            <p></p><b>1</b></div><div>
            <p></p><b>2</b></div><div>
            <p></p><b>3</b></div><div>
            <p></p><b>4</b></div><div>
            <p></p><b>5</b></div><div>
            <p></p><b>6</b></div><div>
            <p></p><b>7</b></div><div>
            <p></p><b>8</b></div>
	</div>
</div>
<script>
    var oBox = $('.box');
    var oBoxWidth = $('.box').width();

    oBox.find('div').on('click',function(){
	var thisWidth = $(this).width();
	var moveLeft = this.offsetLeft;
	if(oBoxWidth<moveLeft+thisWidth){
	    oBox.animate({scrollLeft:moveLeft});
	}else{
	    oBox.animate({scrollLeft:0});	
	}
    });
</script>
</body>
</html>

运行代码,就可以看到,我们点击哪个div标签,该div标签就会滑到最前面的位置,当我们点击最后几个div标签,也不会说滑动位置过大,导致后面空白,如果我们用js解决的话,也还要去限制他的最大滑动距离,而通过css滚动,不需要担心这问题。

nicescroll滑动到底部 滑动css_nicescroll滑动到底部_02