一、存储–服务器环境下
1. cookie
-
cookie存储在本地,容量4k,在同源的http请求时携带传递,损耗带宽。最好的使用方法:设置访问的路径和设置过期时间
-
使用
- 设置cookie: $.cookie(‘mycookie’,‘ok’,{expires:7,path:’/’}) 在/目录下设置一个名为:mycookie值为ok的cookie,保存时间为7天
- 读取cookie: $.cookie(“mycookie”); //ok
-
查看cookie是否设置好了
-
实例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>cookie存储</title> <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script> <script type="text/javascript" src="js/jquery.cookie.js"></script> <script type="text/javascript"> // 属性名 属性值 {expires:存储时间,path:存储路径} // 存cookie $.cookie('mycookie','ok',{expires:7,path:'/'}) // 读取cookie var mycookie = $.cookie("mycookie"); alert(mycookie); // ok </script> </head> <body> <h1>测试页面</h1> </body> </html>
2.webstorage
-
localStorage
永久存储,存储在本地,容量是5M/更大,http请求中不携带,在所有同源窗口共享。 -
sessionStorage
本地存储,5M/更大,请求中不携带,在同源的当前窗口关闭后失效 -
实例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>存储文件</title> <script type="text/javascript"> // 设置 // 永久存储 localStorage.setItem('dat','456'); // localStorage.dat = '456'; //获取 alert(localStorage.dat); // 删除 // localStorage.removeItem("dat"); // 关闭浏览器后失效 sessionStorage.setItem('dat2','789'); </script> </head> <body> <h1>测试webstorage</h1> </body> </html>
浏览器中查看:
3. 一段时间弹出一次的弹窗
- 代码
效果:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>页面弹窗</title> <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script> <script type="text/javascript" src="js/jquery.cookie.js"></script> <script type="text/javascript"> $(function(){ // 先看看有没有设置cookie var hasread = $.cookie('hasread'); // alert(hasread); // 没有就是undefined // 判断是否存储了cookie,没有就弹出弹框 if (hasread == undefined){ // alert("没有cookie"); $(".pop_con").show(); // 用户点击知道后 存储cookie 把弹框关掉 $('#userread').click(function(){ $.cookie('hasread','read',{expires:7,path:'/'}); $('.pop_con').hide(); }); $('#close').click(function(){ $('.pop_con').hide(); }); } }); </script> <style type="text/css"> .pop{ /*弹框*/ position: fixed; width:500px; height: 300px; background-color: #fff; border:3px solid #000; left:50%; top:50%; margin-left:-250px; margin-top: -150px; z-index:9999; } .mask{ /*弹框背景*/ position: fixed; width: 100%; height:100%; background-color: #000; opacity: 0.3; /*兼容IE*/ filter:alpha(opacity=30); left:0; top:0; z-index:9998; } .pop_con{ /*默认为隐藏*/ display: none; } .close{ float: right; font-size: 30px; text-decoration: none; } </style> </head> <body> <div class="pop_con"> <div class="pop"> 亲!本网站最近有优惠活动!请关注! <a href="#" id="close" class="close">×</a> <a href="javascript:;" id="userread">朕晓得了</a> </div> <!-- 弹窗后的背景 --> <div class="mask"></div> </div> <h2>网站内容</h2> </body> </html>
当点击知道了之后会储存一个cookie储存7天,7天之内放问这个页面弹窗不会出现
存储的cookie
二、移动端幻灯片及整体制作
-
使用swiper.jquery.min.js
-
幻灯片的结构要是这种的
<div class="swiper-container"> <div class="swiper-wrapper"> <!-- 幻灯片 --> <div class="swiper-slide"><a href="#"><img src="images/slide.jpg" alt="幻灯片"></a></div> <div class="swiper-slide"><a href="#"><img src="images/slide.jpg" alt="幻灯片"></a></div> <div class="swiper-slide"><a href="#"><img src="images/slide.jpg" alt="幻灯片"></a></div> </div> <div class="swiper-pagination"></div> <!-- 没有箭头就注释掉下面的 --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> </div>
- 即:
- 幻灯片的整个结构要用swiper-container类样式包起来
- 照片的部分要用swiper-wrapper这个包起来,每张照片要用swiper-slide包起来
- 左右箭头分贝用:swiper-button-prev和swiper-button-next的类样式
- 底部的小圆点用:swiper-pagination样式
- js控制代码
// 实例化一个类对象 // 前四个字符串对应的容器名 以及 小圆点 左右箭头 var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', prevButton: '.swiper-button-prev', nextButton: '.swiper-button-next', initialSlide :0, // 初始幻灯片 0开始 paginationClickable: true, // 小圆点是否可以点 loop: true, // 首尾相连的循环 , 如果是false 他就从左边回去了 autoplay:3000, // 设置多长时间间隔播放一张 // 用户操作后是否自动播放,false:自动播放,true:操作后不播放 autoplayDisableOnInteraction:true });
- 即:
-
整体代码的实现:
index.html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <link rel="stylesheet" type="text/css" href="css/reset.css"> <!-- 要放在main的上面 --> <link rel="stylesheet" type="text/css" href="css/swiper.min.css"> <link rel="stylesheet" type="text/css" href="css/main.css"> <script type="text/javascript" src="js/set_root.js"></script> <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script> <script type="text/javascript" src="js/swiper.jquery.min.js"></script> <script type="text/javascript"> $(function(){ // 实例化一个类对象 // 前四个字符串对应的容器名 以及 小圆点 左右箭头 var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', prevButton: '.swiper-button-prev', nextButton: '.swiper-button-next', initialSlide :0, // 初始幻灯片 0开始 paginationClickable: true, // 小圆点是否可以点 loop: true, // 首尾相连的循环 , 如果是false 他就从左边回去了 autoplay:3000, // 设置多长时间间隔播放一张 // 用户操作后是否自动播放,false:自动播放,true:操作后不播放 autoplayDisableOnInteraction:true }); }); </script> <title>天天生鲜---移动端</title> </head> <body> <div class="main_wrap"> <!-- 头部 --> <div class="header clearfix"> <a href="#" class="logo"> <img src="images/logo.png" alt="logo"> </a> <a href="#" class="search"></a> </div> <!-- 中间 --> <div class="center_con"> <!-- 幻灯片 + 小圆点(自动可以生成) --> <div class="slide"> <div class="swiper-container"> <div class="swiper-wrapper"> <!-- 幻灯片 --> <div class="swiper-slide"><a href="#"><img src="images/slide.jpg" alt="幻灯片"></a></div> <div class="swiper-slide"><a href="#"><img src="images/slide.jpg" alt="幻灯片"></a></div> <div class="swiper-slide"><a href="#"><img src="images/slide.jpg" alt="幻灯片"></a></div> </div> <div class="swiper-pagination"></div> <!-- 没有箭头就注释掉下面的 --> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> </div> <!-- <img src="images/slide.jpg" alt="幻灯片"> --> </div> <!-- 菜单 --> <!-- .menu_con>ul.menu>(li>a+h3{水果})*8 --> <div class="menu_con clearfix"> <ul class="menu"> <li> <a href="#"></a> <h2>水果</h2> </li> <li> <a href="#"></a> <h2>海鲜</h2> </li> <li> <a href="#"></a> <h2>肉类</h2> </li> <li> <a href="#"></a> <h2>禽蛋</h2> </li> <li> <a href="#"></a> <h2>蔬菜</h2> </li> <li> <a href="#"></a> <h2>速冻</h2> </li> <li> <a href="#"></a> <h2>热卖</h2> </li> <li> <a href="#"></a> <h2>新品</h2> </li> </ul> </div> <!-- 公共模块 -- 展示 --> <div class="common_model clearfix"> <div class="common_title"> <h3>新鲜水果</h3> <a href="#">更多 ></a> </div> <a href="#" class="banner"> <img src="images/banner.jpg" alt="banner"> </a> <ul class="goods_list"> <li> <a href="#" class="goods_link"> <img src="images/goods019.jpg" alt="商品"> </a> <h4>新西兰皇家草莓</h4> <p class="unit">1kg/提</p> <p class="price">¥30.00</p> <a href="#" class="add_chart"></a> </li> <li> <a href="#" class="goods_link"> <img src="images/goods019.jpg" alt="商品"> </a> <h4>新西兰皇家草莓</h4> <p class="unit">1kg/提</p> <p class="price">¥30.00</p> <a href="#" class="add_chart"></a> </li> <li> <a href="#" class="goods_link"> <img src="images/goods019.jpg" alt="商品"> </a> <h4>新西兰皇家草莓</h4> <p class="unit">kg/提</p> <p class="price">¥30.00</p> <a href="#" class="add_chart"></a> </li> </ul> </div> <div class="common_model clearfix"> <div class="common_title"> <h3>新鲜水果</h3> <a href="#">更多 ></a> </div> <a href="#" class="banner"> <img src="images/banner.jpg" alt="banner"> </a> <ul class="goods_list"> <li> <a href="#" class="goods_link"> <img src="images/goods019.jpg" alt="商品"> </a> <h4>新西兰皇家草莓</h4> <p class="unit">1kg/提</p> <p class="price">¥30.00</p> <a href="#" class="add_chart"></a> </li> <li> <a href="#" class="goods_link"> <img src="images/goods019.jpg" alt="商品"> </a> <h4>新西兰皇家草莓</h4> <p class="unit">1kg/提</p> <p class="price">¥30.00</p> <a href="#" class="add_chart"></a> </li> <li> <a href="#" class="goods_link"> <img src="images/goods019.jpg" alt="商品"> </a> <h4>新西兰皇家草莓</h4> <p class="unit">kg/提</p> <p class="price">¥30.00</p> <a href="#" class="add_chart"></a> </li> </ul> </div> </div> <!-- 尾部 --> <ul class="footer"> <li> <a href="#"></a> <h2>首页</h2> </li> <li> <a href="#"></a> <h2>分类</h2> </li> <li> <a href="#"></a> <h2>购物车</h2> </li> <li> <a href="#"></a> <h2>我的</h2> </li> </ul> </div> </body> </html>
reset.css
/* 将有格式的标签清除格式 */ /* 将标签默认的间距设为0 */ body, p, h1, h2, h3, h4, h5, h6, ul, dl, dt,form, input{ margin: 0px; padding: 0px; } /* 清除ul的小圆点 ol也有,如果用就加 */ ul{ list-style: none; } /* 去掉默认的下划线 */ a{ text-decoration: none; } /* 去掉倾斜 */ em{ font-style: normal; } /* 在ie中图片做链接会有一个默认的边框 去掉它 */ img{ border:0px; } /* 去掉默认的大小 -- 让h标签继承body的font-size的设置 */ h1, h2, h3, h4, h5, h6{ font-size:100%; font-weight: normal; } /* 清除浮动、解决margin-top的塌陷 */ .clearfix:before, .clearfix:after{ content: ""; display: table; } .clearfix:after{ clear:both; } .clearfix{ /* ie的bug*/ zoom:1; } /* 左浮动 */ .fl{ float:left; } /* 右浮动 */ .fr{ float: right; }
main.css
.main_wrap{ position:absolute; background-color:gold; right:0; left:0; top:0; bottom:0; } /* 顶部样式 */ .header{ height:2.5rem; background-color: #37ab40; position: relative; /*搜索框*/ } .logo{ display: block; width:4.8rem; height:1.9rem; margin:0.3rem auto 0; } .logo img{ width:100%; } .search{ width:1.6rem; height: 1.6rem; position: absolute; /*绝对定位*/ right:0.9rem; top:0.4rem; background:url(../images/icons.png) left top no-repeat; background-size:1.5rem 40.0rem; /*缩放一半*/ } /* 中间的样式 */ .center_con{ position:absolute; left:0px; right:0px; top:2.5rem; bottom:2.5rem; background-color:#efefef; /* y轴方向内容多给滚动条 x轴方向内容多了直接裁剪 只自动出现竖向滚动条,不出现横向滚动条 */ overflow-y: auto; overflow-x: hidden; padding-bottom: 0.5rem; } /*幻灯片*/ .slide{ height:6.75rem; } .slide img{ width:100%; height:100%; } .menu_con{ height:9.25rem; margin-top:0.5rem; background-color:#fff; overflow: hidden; } .menu_con ul{ width:18rem; height:8.375rem; margin:0.6rem auto 1.375rem; /*background-color:gold;*/ } .menu li{ width:3.0rem; height:4.05rem; float:left; /*background-color:pink;*/ margin-right: 1.5rem; } .menu li a{ display: block; height:2.50rem; background:url(../images/icons.png) 0.7rem -2rem no-repeat; background-size:1.5rem 40.0rem; /*缩放一半*/ } .menu li:nth-child(2) a{ background-position: 0.7rem -4.5rem; } .menu li:nth-child(3) a{ background-position: 0.7rem -6.9rem ; } .menu li:nth-child(4) a{ background-position: 0.7rem -9.35rem ; } .menu li:nth-child(5) a{ background-position: 0.7rem -11.55rem ; } .menu li:nth-child(6) a{ background-position: 0.7rem -14rem ; } .menu li:nth-child(7) a{ background-position: 0.7rem -16.5rem ; } .menu li:nth-child(8) a{ background-position: 0.7rem -18.7rem ; } .menu h2{ height: 1.25rem; font: bold 13px/1.25rem "Microsoft Yahei"; color:#666; text-align: center; } /*公共模块 展示*/ .common_model{ height:17.25rem; background-color:#fff; margin-top: 0.5rem; } .common_title{ width:17.75rem; height:0.9rem; /*background-color:gold;*/ margin:0.8rem auto 0; /*border-left:2px ;*/ } .common_title h3{ float: left; font:bold 15px/0.9rem "Microsoft Yahei" ; color:#fbc83d; border-left:0.25rem solid #fbc83d; text-indent: 0.4rem; } .common_title a{ float:right; font:normal 12px/0.9rem "Microsoft Yahei"; color:#7f7f7f; } .banner{ display: block; width:17.75rem; height:4.5rem; margin:0.5rem auto 0; background:gold; } .banner img{ width:100%; height:100%; } .goods_list{ width:17.75rem; height:9.325rem; /*background-color:gold;*/ margin:0.5rem auto 0; } .goods_list li{ width:5.9rem; height:9.325rem; border-right:1px solid #e7e7e7; float:left; box-sizing: border-box; position: relative; } /*选择器,选择最后一个*/ .goods_list li:last-child{ border-right: 0; } .goods_link{ display: block; width:4.5rem; height:4.5rem; margin:0.375rem auto 0; } .goods_link img{ width:100%; height:100%; } .goods_list h4{ font:normal 15px/15px "Microsoft Yahei"; width:5.0rem; margin:0.925rem auto 0; /* 多的文字是省略号 1. 宽度固定 2. overflow:hidden; 3. 强制文字不换行 white-space nowrap 4. 超出部分显示省略号 text-overflow: ellipsis; */ overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } .unit{ width:5.0rem; font:normal 12px/12px "Microsoft Yahei"; color:#bbb; margin:0.8rem auto 0; } .price{ width:5.0rem; font:bold 12px/12px "Microsoft Yahei"; color:#ff4400; margin:0.5rem auto 0; } .add_chart{ position: absolute; width:2.0rem; height: 2.0rem; background:url(../images/icons.png) 0.25rem -21.5rem no-repeat; background-size:1.5rem 40.0rem; right: 0.675rem; bottom: 0; } /*尾部的样式*/ .footer{ position:absolute; left:0px; bottom:0px; width:100%; height:3rem; background-color:lightgreen; } /*流体布局*/ .footer li{ width:25%; height:2.5rem; float:left; } .footer li a{ display: block; width:1.6rem; height:1.6rem; margin:0.35rem auto 0; background:url(../images/icons.png) left -24rem no-repeat; background-size:1.5rem 40.0rem; } .footer li:nth-child(2) a{ background-position: left -26.69rem; } .footer li:nth-child(3) a{ background-position: left -29.1rem; } .footer li:nth-child(4) a{ background-position: left -31.4rem; } .footer li h2{ text-align: center; font:normal 12px/0.8rem "Microsoft Yahei"; color:#949392; margin-top: 0.2rem; } /* 幻灯片上的箭头 */ .swiper-button-next,.swiper-button-prev { position: absolute; top: 50%; width: 20px; height: 25px; margin-top: -22px; z-index: 10; cursor: pointer; -moz-background-size: 27px 44px; -webkit-background-size: 27px 44px; background-size:20px 35px; background-position: center; background-repeat: no-repeat } .swiper-pagination { text-align:right; } .swiper-pagination-bullet-active { /*小圆点的颜色*/ background: yellowgreen; } .swiper-container-horizontal>.swiper-pagination-bullets.swiper-pagination span:last-child{ /*最后一个span*/ margin-right: 20px; }
效果图: