• js小滑块


    <div class="bigbox">
    		<div class="smallbox">
    			<ul>
    				<li>
    					<div class="inner_content"><img src="http://via.placeholder.com/600x300" alt=""></div>
    					<div class="inner_message">
    						<h2>this is a title</h2>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    					</div>
    				</li>
    				<li>
    					<div class="inner_content"><img src="http://via.placeholder.com/600x300" alt=""></div>
    					<div class="inner_message">
    						<h2>this is a title</h2>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    					</div>
    				</li>
    				<li>
    					<div class="inner_content"><img src="http://via.placeholder.com/600x300" alt=""></div>
    					<div class="inner_message">
    						<h2>this is a title</h2>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    					</div>
    				</li>
    				<li>
    					<div class="inner_content"><img src="http://via.placeholder.com/600x300" alt=""></div>
    					<div class="inner_message">
    						<h2>this is a title</h2>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    					</div>
    				</li>
    				<li>
    					<div class="inner_content"><img src="http://via.placeholder.com/600x300" alt=""></div>
    					<div class="inner_message">
    						<h2>this is a title</h2>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    					</div>
    				</li>
    				<li>
    					<div class="inner_content"><img src="http://via.placeholder.com/600x300" alt=""></div>
    					<div class="inner_message">
    						<h2>this is a title</h2>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    					</div>
    				</li>
    				<li>
    					<div class="inner_content"><img src="http://via.placeholder.com/600x300" alt=""></div>
    					<div class="inner_message">
    						<h2>this is a title</h2>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    						<p>Lorem ipsum dolor sit amet.</p>
    					</div>
    				</li>
    			</ul>
    		</div>
    		<div class="arrow_left"><</div>
    		<div class="arrow_right">></div>
    	</div>
    

      

     1 *{
     2         margin: 0;
     3         padding: 0;
     4     }
     5     img{
     6         width: 100%;
     7         height: auto;
     8     }
     9         .bigbox{
    10             position: relative;
    11         }
    12         .smallbox{
    13             width: 100%;
    14             overflow: hidden;
    15         }
    16         .smallbox ul{
    17             width: 400%;
    18             /*取决你放几屏幕的宽度*/
    19         }
    20         .smallbox ul li{
    21             width: 12.5%;
    22             /*取决你的一屏幕下的需要展示的数目*/
    23             /*因为demo是四屏幕,100/4/2*/
    24             float: left;
    25             overflow: hidden;
    26         }
    27         .inner_content,.inner_message{
    28             float: left;
    29             width: 50%;
    30         }
    31         .arrow_left{
    32             position: absolute;
    33             top:50%;
    34             left: 0;
    35             font-size:2em;
    36             transform: translateY(-50%); 
    37         }
    38         .arrow_right{
    39             position: absolute;
    40             top: 50%;
    41             right: 0;
    42             font-size: 2em;
    43             transform: translateY(-50%);
    44         }
    45         @media screen and (max- 900px){
    46             /*写媒体查询的时候记得要先在head标签声明好视口*/
    47             .smallbox ul li{
    48                 width: 14.285714%;
    49             }
    50             .smallbox ul{
    51             width: 700%;
    52             }
    53             .arrow_left,.arrow_right{
    54                 display: none;
    55             }
    56         }
    View Code
     1 var smallbox_width = document.querySelector(".smallbox").offsetWidth;
     2         var smallbox_ul = document.querySelector(".smallbox ul");
     3         var arrow_right = document.querySelector(".arrow_right");
     4         var arrow_left = document.querySelector(".arrow_left");
     5         smallbox_ul.style.position="relative";
     6         smallbox_ul_width = smallbox_ul.offsetWidth;
     7         // ul滑块的总的宽度
     8         screenWidth = window.screen.width;
     9         // 屏幕的宽度,原本想利用滑块的宽度除以屏幕的宽度得到需要的屏幕数,用来给index定值,可是好像获取的屏幕像素和实际预期的居然不一样!!!
    10         var index =0;
    11 
    12         arrow_right.onclick=towardRight;
    13         arrow_left.onclick=towardLeft;
    14         tapEvents(smallbox_ul);
    15         console.log(screenWidth)
    16         console.log(smallbox_ul_width)
    17         function tapEvents(obj){
    18             var startX = null;
    19             var endX = null;
    20             obj.addEventListener("touchstart",function(e){
    21                 startX = e.touches[0].clientX;
    22             });
    23             obj.addEventListener("touchmove",function(e){
    24                 endX = e.touches[0].clientX - startX-30;
    25             });
    26             obj.addEventListener("touchend",function(){
    27                 if(endX>0){
    28                     towardLeft();
    29                 }else{
    30                     towardRight();
    31                 }
    32             })
    33         }
    34         function towardLeft(){
    35             index--;
    36             if(index<0){
    37                 index = Math.ceil(smallbox_ul_width/smallbox_width)-1;
    38             }
    39                 move_box(smallbox_ul,-index*smallbox_width);
    40         }
    41         function towardRight(){
    42             index++;
    43             if(index>(Math.ceil(smallbox_ul_width/smallbox_width)-1)){
    44                 index=0;
    45             }
    46             move_box(smallbox_ul,-index*smallbox_width);
    47         }
    48 
    49 
    50         function move_box(ele,target){
    51             var timer = null;
    52             clearInterval(timer);
    53             timer = setInterval(fn1,20);
    54             function fn1(){
    55                 var step = ele.offsetLeft - target>0?-10:10;
    56                 var val = ele.offsetLeft - target;
    57                 ele.style.left = ele.offsetLeft +step+"px";
    58                 if(Math.abs(val)<=step){
    59                     ele.style.left = target+"px";
    60                     clearInterval(timer);
    61                 }
    62             }
    63         }
    View Code
  • 相关阅读:
    同台电脑 多Git账号同时使用
    netty对http协议解析原理解析(转载)
    Netty 线程模型与Reactor 模式
    增量/存量数据按时间维度分组
    网易技术分享:Nginx缓存引发的跨域惨案
    全面剖析Redis Cluster原理和应用
    聊聊阿里社招面试,谈谈“野生”Java程序员学习的道路
    美团点评基于 Flink 的实时数仓建设实践
    美团技术分享:大众点评App的短视频耗电量优化实战
    美团技术分享:美团深度学习系统的工程实践
  • 原文地址:https://www.cnblogs.com/cyany/p/7696107.html
Copyright © 2020-2023  润新知