# shopping **Repository Path**: houyi_studio_admin/shopping ## Basic Information - **Project Name**: shopping - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-10-29 - **Last Updated**: 2023-10-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #
项目实训 - 品优购网站开发
## 1.项目开发准备 ### 1.1 项目参考网址 ​ http://shop.welite.top/ ### 1.2 创建项目目录 ```bash mkdir pinyoushopping cd pinyoushopping mkdir style images javascript pages touch index.html 说明: 1)在项目开发中,项目的所有文件夹和文件名要求用英文命名(尽量用小写),不能出现中文或其他非法字符。 2)style用来放CSS文件,images用来放网站需要的图片文件,javascript放JS文件,pages放子页(.html)文件。 3)index.html(项目的入口文件)必须放在项目的根目录下。 ``` ## 2.项目实施 ### 2.1 首页 #### 2.1.1 首页结构分析 ##### 2.1.1.1 头部(header) ##### 2.1.1.2 主体部分(main) ##### 2.1.1.3 尾部(footer) #### 2.1.2 引入reset.css样式重置文件 ​  参考网址:https://www.cnblogs.com/dhy-javascript/p/6186082.html   不同的浏览器的初始样式可能不一样,为了统一样式,需要引入reset.css文件,同时在reset.css文件中还可以添加公共样式。该文件必须在所有.html文件引入,且必须放在所有引入的CSS文件的最前面。 ```css body,button,dd,dl,dt,form,h1,h2,h3,h4,h5,h6,hr,input,li,ol,p,td,textarea,th,ul,img { margin: 0; /* 外边距 */ padding: 0;/* 内边距 */ } body,button,input,select,textarea { font: 14px/1.5 微软雅黑; /* 字号为14px,行距1.5倍,字体为微软雅黑 */ } h1,h2,h3,h4,h5,h6 { font-size: 100%; /* 原始输出标题标签的默认字号 */ } ol,ul { list-style: none; /* 去掉ol前的编号和ul前的点 */ } a { color: #333; text-decoration: none; /* 去掉a标签的下划线 */ } img { border: 0; /* 兼容处理,在IE低版本中,图像会出现边框,0表示取消边框 */ } button,input,select,textarea { font-size: 100%; outline: none; /* 取消表单组件的边框 */ } table {/* 设置表格为单线边框 */ border-collapse: collapse; border-spacing: 0; } ``` 引入: ```html ``` #### 2.1.3 首页头部设置 index.html ```html ``` ##### 2.1.3.1 设置标题 ```html 品优购-综合网购首选-正品低价、品质保障、配送及时、轻松购物! ``` ##### 2.1.3.2 设置网站图标 ```html ``` ##### 2.1.3.3 设置网站关键字 用于SEO关键字抓取。 > SEO(Search Engine Optimization):汉译为搜索引擎优化。是一种方式:利用搜索引擎的规则提高网站在有关搜索引擎内的自然排名。目的是让其在行业内占据领先地位,获得品牌收益。很大程度上是网站经营者的一种商业行为,将自己或自己公司的排名前移。 ```html ``` ##### 2.1.3.4 设置网站描述 用于SEO关键字抓取。 ```html ``` ##### 2.1.3.5 浏览器设置 ```html ``` #### 2.1.4 首页整体布局 ```html
``` #### 2.1.5 首页header部分 ##### 2.1.5.1 header整体布局 ```html
``` ##### 2.1.5.2 创建样式文件 style->index.css文件(首页样式文件)。 style->common.css(公共样式文件),用来存放网站中的公共样式。 ```css /* 设置版心,并居中 */ .center{ width: 1200px; /* 版心 */ margin: 0 auto; /* 盒子(一个标签就是一个盒子)水平居中 */ } ``` ##### 2.1.5.3 【首页头部】快捷菜单栏布局 **要实现效果:** **HTML布局(index.html):** ```html
品优购欢迎您!请 登录 免费注册
``` ##### 2.1.5.4 【首页头部】快捷菜单栏样式 **通用样式(common.css):** ```css .center{ width: 1200px; /* 版心 */ margin: 0 auto; /* 盒子(一个标签就是一个盒子)水平居中 */ } /* 左浮动 */ .fl{ float: left; } /* 右浮动 */ .fr{ float: right; } :root{ /* 设置属性值变量 */ --bgc: #f1f1f1; --cl: #b1191a; } ``` **CSS样式(index.css):** ```css /* 快捷菜单栏 */ .shortcut{ width: 100%; height: 33px; background-color: #f1f1f1; line-height: 33px; /* 垂直居中 */ font-size: 14px; } .welcome a{ color: var(--cl); /* 使用属性变量 */ } .welcome a:hover{ color: #333; } .shortcut-right ul li{ float: left; /* 让li标签的内容排在一行上 */ } .shortcut-right a:hover{ color: #666; } .shortcut-right span{ margin: 0 10px 0 5px;/* 设置外边距。margin:10px;表示上下左右都为10px;margin:10px 20px;表示上下为10px,左右为20px;margin:10px 20px 30px;表示上为10px,左右为20px,下为30px;margin:10px 20px 30px 40px;表示上为10px,右为20px,下为30px,左为40px。*/ } ``` ##### 2.1.5.5 【首页头部】Logo区布局 **实现效果:** **HTML布局(index.html):** ```html

logo

我的购物车 0
``` **引入Iconfont图标:** ​ 步骤: ​ 1)注册并登录; ​ 2)新建项目; ​  3)搜索图标,并加入到购物车;   4)将购物车中的图标添加到项目;   5)下载图标文件到本地;   6)将字体文件和iconfont.css文件复制到项目对应目录中;   7)引入样式文件; ```html ```   8)使用图标; ```html ```   9)效果展示。 **CSS样式(index.css):** ```css /* Logo区 */ .logo-area{ overflow: hidden; /* 清除浮动 */ } .logo-area h1{ margin: 25px 176px 25px 0; } .logo-area h1:after{ content: '品优购'; display: none; text-indent: -9999; overflow: hidden; } .search input{ width: 455px; height: 35px; border: 2px solid var(--cl); margin-top: 30px; padding-left: 10px; box-sizing: border-box; /* 盒子的宽高为文本设定的宽高 */ } .search input::placeholder{ font-size: 13px; } .search button{ width: 84px; height: 35px; background-color: var(--cl); color: #fff; border: 0; margin-left: -4px; } .cart{ width: 140px; height: 35px; margin: 30px 66px 0 0; border: 1px solid #dfdfdf; background-color: #f7f7f7; line-height: 35px; /* 垂直居中 */ text-align: center; /* 水平居中 */ position: relative; /* 相对定位 */ } .cart i:first-child{ color: #da5555; margin-right: 5px; } .cart span{ width: 14px; height: 14px; display: block; margin-left: 5px; background-color: #e60012; color: #fff; border-radius: 7px 7px 7px 0; line-height: 14px; position: absolute; /* 绝对定位 */ top: -6px; right: 22px; } ``` ##### 2.1.5.6 【首页头部】导航栏 **实现效果:** **HTML布局(index.html):** ```html ``` **CSS样式(index.css):** ```css /* 导航栏 */ nav{ width: 100%; height: 45px; font-size: 16px; border-bottom: 2px solid var(--cl); } .nav-title{ width: 210px; height: 45px; background-color: var(--cl); color: #fff; line-height: 45px; text-align: center; } .nav-classic-list{ width: 210px; height: 400px; font-size: 14px; background-color: var(--classic-bg); position: relative;/* 分类子菜单定位参考 */ } .nav-classic-list>li{/* >:表示查找子元素 */ /*position: relative; 相对自己本来的位置做位移。位移有4个方向词:left、right、top、bottom,left和right不能同时使用,top和bottom不能同时使用。*/ /*overflow: hidden; 清除浮动 */ transition: all .5s; /* 过渡(补间)动画,all表示所有属性的改变,.5s表示动画过渡时间*/ } .nav-classic-list>li:hover{ background-color: #fff; /* 在li标签上悬停时,li的背景色变成白色 */ } .nav-classic-list>li:hover .classic-name{ color: var(--classic-bg); /* 在li标签上悬停时,改变.classic-name文字的颜色 */ padding-left: 20px; } .nav-classic-list>li:hover .icon{ display: none; } .nav-classic-list>li:hover .nav-classic-item{ display: block; } .classic-name{ color: #fff; padding: 10px; transition: all .5s; } .classic-name span{ font-size: 14px;/* 利用iconfont加载的符号可以像文字一样处理 */ position: absolute; /* 绝对定位:相对于父元素位移,父元素一般要设置为相对定位。绝对定位会脱离文档流,所以必须在父元素上清除浮动。*/ right: 10px; } .nav-classic-item{ width: 520px; height: 400px; background-color: var(--bgc1); display: none; position: absolute; left: 210px; top: 0px; z-index: 999;/* 调整排列次序,999表示调到最高的层级 */ } .nav-classic-item li:hover .goods-name{ color: #999; } .nav-classic-item img{ height: 44px; margin: 10px 10px 10px 15px; } .goods-name{ position: relative; top: -25px; } .nav-main{ overflow: hidden; position: relative; bottom: -10px; } .nav-main li{ margin: 0 25px; float: left; } .nav-main li:first-child{ margin-left: 30px; } .nav-main a:hover{ color: #ccc; } .nav-banner{ position: relative; overflow: hidden; } /* banner */ .banner{ position: relative; } .swiper { width: 990px; height: 400px; position: absolute; left: 210px; } .swiper img{ width: 100%; height: 100%; } ``` **轮播图JavaScript逻辑处理:** ```html ``` #### 2.1.6 首页主体部分 ##### 2.1.6.1 实现效果 ##### 2.1.6.2 【首页主体】HTML布局 ```html
精品推荐
``` ##### 2.1.6.3 【首页主体】CSS样式 通用CSS样式(common.css): ```css body{ color: #333; font-family: 微软雅黑; } .center{ width: 1200px; /* 版心 */ margin: 0 auto; /* 盒子(一个标签就是一个盒子)水平居中 */ } /* 左浮动 */ .fl{ float: left; } /* 右浮动 */ .fr{ float: right; } :root{ /* 设置属性值变量,变量以"--"开头*/ --bgc: #f1f1f1; --cl: #b1191a; --classic-bg: #c81623; --bgc1: #f7f7f7; } ``` **主体部分CSS样式:** ```css /* 主体 */ .product{ margin-top: 20px; } .product-title{ height: 35px; line-height: 35px; border-bottom: 2px solid var(--cl); } .title{ font-size: 18px; color: var(--classic-bg); margin-right: 10px; } .pro-classic li{ float: left; margin-left: 30px; } .pro-classic li:hover a{ color: #999; } .more{ margin-right: 10px; line-height: 26px; } .more:hover a{ color: #999; } .product-list{ margin-top: 10px; display: flex;/* 弹性布局,默认横向排列,主要用于九宫格图像布局,它会自适应。*/ justify-content: space-between; /* 将剩余的空间进行等分 */ } .product-list1{ margin-top: 10px; } .product-item{ width: 220px; height: 285px; background-color: var(--bgc1); text-align: center; } .product-list1 .product-item{ display: inline-block; /* 将块级标签转换为行内块级标签,目的是将li标签排在一行上。用float:left;也可以实现这个效果,但必须在父级要清除浮动。 */ margin-right: 20px; } .product-item img{ width: 144px; height: 142px; margin-top: 50px; } .pro-name{ font-size: 16px; } .pro-price{ line-height: 40px; color: var(--classic-bg); } ``` #### 2.1.7 首页尾部 ##### 2.1.7.1 实现效果 ##### 2.1.7.2 【首页尾部】HTML布局 ```html ``` ##### 2.1.7.3 【首页尾部】CSS样式 ```css /* footer */ footer{ width: 100%; background-color: #f5f5f5; font-size: 14px; margin-top: 20px; } .service, .help{ display: flex; justify-content: space-around; border-bottom: 1px solid #ccc; } .service{ padding: 30px 0; } /* 精灵图:Sprite(雪碧图),把一些小图标文件放在一个文件中(该文件的格式只能是.png(背景透明)),目的是为了减少向服务器发请求的次数,从而提升网站性能。 精灵图操作步骤: 1)绘制精灵图(借助PS),保存为.png文件; 2)用background加载精灵图(不能用标签加载): url(精灵图地址) no-repeat x坐标 y坐标 注意:必须要设置需要获取的图标的宽高。 */ .service li{ width: 190px; } .service li .service-img{ width: 50px; height: 50px; background: url(../images/icons.png) no-repeat; float: left; margin-right: 5px; } .service li:nth-child(1) .service-img{ background-position: -252px -3px; } .service li:nth-child(2) .service-img{ background-position: -255px -54px; } .service li:nth-child(3) .service-img{ background-position: -257px -105px; } .service li:nth-child(4) .service-img{ background-position: -257px -157px; } .service li:nth-child(5) .service-img{ background-position: -256px -208px; } .service-title{ line-height: 1.8; } .help{ padding: 20px 0 10px; border-bottom: 1px solid #ccc; text-align: center; } .help dt{ font-size: 16px; } .help dt,.help dd{ line-height: 1.7; } .help dd:hover a{ color: #999; } .link-copyright{ padding: 20px 0 30px; text-align: center; color: #666; } .link-copyright a{ color: #666; } .link-copyright a:hover{ color: #999; } .link{ line-height: 3; } .link li{ display: inline-block; margin-right: 10px; } .link li:not(:first-child)::before{ /* 伪元素:寄生在实际标签上 */ content: ""; /* content不能省略,否则伪元素会失效 */ display: block; width: 1px; height: 12px; border-right: 1px solid #666; position: relative; /* 调整竖线位置(相对原来位置进行调整) */ left: -9px; top: 27px; } ``` #### 2.1.8 首页侧边栏 ##### 2.1.8.1 实现效果 ##### 2.1.8.2 【首页侧边栏】HTML布局 ```html ``` ##### 2.1.8.3 【首页侧边栏】CSS样式 ```css /* 侧边栏 */ aside{ width: 70px; position: fixed;/* 固定定位,参照文档进行定位 */ left: 0; top: 50%; margin-top: -122px; text-align: center; background-color: #fff; display: none; /* 初始处于隐藏状态 */ } aside ul{ font-size: 12px; } aside li, aside .to-top{ line-height: 35px; } aside li{ border-bottom: 1px solid #ccc; } aside a{ display: block; /* a为行级标签,自身不识别宽高,必须将其转换为块级标签后,让其识别宽高 */ width: 100%; } .active{ background-color: var(--classic-bg); } .active a{ color: #fff; } .to-top:hover a{ color: #999; } ``` ##### 2.1.8.4 【首页侧边栏】实现侧边栏滚动和定位效果 **引入jQuery.js:** ```html ``` **逻辑处理:** ```js // 侧边栏 changeStyle(); $(window).scroll(function(){ // 当窗口滚动条滚动时触发scroll事件 changeStyle(); }) // 抽离公共代码,形成函数,方便多次调用 function changeStyle(){ // 获取滚动条的位置 let pos = Math.floor($(this).scrollTop()); // $(this)表示当前对象window,scrollTop()获取当前滚动条离顶端的距离,Math.floor()向下取整,let用于定义变量 // console.log(pos); // 如果滚动条在500+及以上,则显示侧边栏,否则则隐藏 if(pos >= 500){ $('aside').css('display','block'); } else { $('aside').css('display','none'); } // 根据滚动条位置设置侧边栏样式 if(pos >= 500 && pos < 1070){ // 如果滚动条在[500,1070)区间,则调用函数changePos() changePos(0); } else if(pos >= 1070 && pos < 1250){ // 否则,如果滚动条在[1070,1250)区间 changePos(1); } else if(pos >= 1250 && pos < 1500){ changePos(2); } else if(pos >= 1500 && pos < 1750){ changePos(3); } else if(pos >= 1750 && pos < 1875){ changePos(4); } else if(pos >= 1875){ changePos(5); } } // 函数封装 function changePos(idx){ $('aside li').removeClass('active'); // 移出aside下所有li标签中的active类 $('aside li').eq(idx).addClass('active'); // 为aside下指定的li标签添加active类 } // 回到顶端 $('.to-top').click(function(){ // 当单击class为to-top的标签时,将执行回调函数 // $(window).scrollTop(0); // 设置滚动条移动到窗口顶端 $(window.opera ? 'html' : 'html, body').animate({ // 为所有浏览器添加动画 scrollTop: 0 // 滚动条滚动到顶部 }, 1000) // 滚动条滚动的时间,默认单位为ms }) // 单击li标签,滚动到指定位置 $('aside li').eq(0).click(function(){ scrollLi(700); }) $('aside li').eq(1).click(function(){ scrollLi(1100); }) $('aside li').eq(2).click(function(){ scrollLi(1400); }) $('aside li').eq(3).click(function(){ scrollLi(1600); }) $('aside li').eq(4).click(function(){ scrollLi(1700); }) $('aside li').eq(5).click(function(){ scrollLi(1800); }) function scrollLi(pos){ $(window.opera ? 'html' : 'html, body').animate({ scrollTop: pos }, 1000) } ```