小兔鲜项目-原生JS实现页面登录模块

目录

  • 1. 项目搭建
    • 1.1 在项目开始之前,先做好准备工作,创建这样的文件目录
      • 1.1.1 css文件夹
      • 1.1.2 images文件
      • 1.1.3 uploads文件
    • 1.2 HTML结构
      • 1.2.1 头部结构
      • 1.2.2主体结构
      • 1.2.3 login.html代码示例:
    • 1.3 css样式
      • 1.3.1 页面初始化(base)样式
      • 1.3.2 公共样式(common)样式
      • 1.3.3 首页样式(login)样式
  • 2、JavaScript源码
    • 2.1实现效果:

1. 项目搭建

1.1 在项目开始之前,先做好准备工作,创建这样的文件目录

小兔鲜项目-原生JS实现页面登录模块_第1张图片

1.1.1 css文件夹

包含三个css文件,分别是base.css、common.css、login.css。
小兔鲜项目-原生JS实现页面登录模块_第2张图片

1.1.2 images文件

里面放一些固定的图片

1.1.3 uploads文件

里面放一些固定的图片

1.2 HTML结构

1.2.1 头部结构

在这里插入图片描述

1.2.2主体结构

小兔鲜项目-原生JS实现页面登录模块_第3张图片

1.2.3 login.html代码示例:




    
    
    
    小兔鲜儿-新鲜、惠民、快捷!
    
    
    
    
    
    
    
    
    
    
    
    



    
    
    
    
    
    

1.3 css样式

1.3.1 页面初始化(base)样式

在默认的浏览器中有很多默认的样式,所以我们要对其进行清除并且设置自己的默认样式
代码示例:

  box-sizing: border-box;
}

/* 去除常见标签默认的 margin 和 padding */
body,
h1,
h2,
h3,
h4,
h5,
h6,
p,
ul,
ol,
li,
dl,
dt,
dd,
input {
  margin: 0;
  padding: 0;
}

/* 设置网页统一的字体大小、行高、字体系列相关属性 */
body {
  box-sizing: border-box;
  font: 16px/1.5 "Helvetica Neue", Helvetica, Arial, "Microsoft Yahei",
    "Hiragino Sans GB", "Heiti SC", "WenQuanYi Micro Hei", sans-serif;
  color: #333;
}

/* 去除列表默认样式 */
ul,
ol {
  list-style: none;
}

/* 去除默认的倾斜效果 */
em,
i {
  font-style: normal;
}

/* 去除a标签默认下划线,并设置默认文字颜色 */
a {
  text-decoration: none;
  color: #333;
}

/* 设置img的垂直对齐方式为居中对齐,去除img默认下间隙 */
img {
  vertical-align: middle;
}

/* 去除input默认样式 */
input {
  border: none;
  outline: none;
  color: #333;
}



/* 双伪元素清除浮动 */
.clearfix::before,
.clearfix::after {
  content: "";
  display: table;
}

.clearfix::after {
  clear: both;
}

1.3.2 公共样式(common)样式

在这里插入图片描述

.w {
    width: 1240px;
    margin: 0 auto;
}

/* 快捷导航栏开始 */
.shortcut {
    height: 52px;
    background-color: #333;
    line-height: 52px;
}
.s {
    position: absolute;
    left: 0;
    top: 0;
    
}
.shortcut ul {
    float: right;
}

.shortcut li {
    float: left;
}

.shortcut li:nth-child(13)::before {
    content: '';
    display: inline-block;
    vertical-align: middle;
    width: 11px;
    height: 16px;
    margin-right: 5px;
    background-color: #27ba9b;
    background: url(../images/sprites.png) no-repeat -160px -70px;
}

.sep {
    color: #666;
    margin: 0 15px;
}

.shortcut li a {
    color: #dcdcdc;
    font-size: 14px;
}

.shortcut li a:hover {
    color: #27ba9b;
}

/* 快捷导航栏结束 */
/* 主导航栏开始 */
.main_nav {
    height: 130px;
    padding: 30px 0;

}

.logo {
    float: left;
    width: 207px;
    height: 70px;

}

.logo h1 a {
    display: block;
    width: 207px;
    height: 70px;
    font-size: 0;
    background: url(.././images/logo.png) no-repeat;
    background-size: 100%;
}

.nav {
    float: left;
    margin-top: 5px;
    margin-left: 35px;
}

.nav ul li {
    float: left;
    margin: 24px;
}

.nav ul li a {
    padding-bottom: 5px;
}

.nav ul li:hover a {
    border-bottom: 1px solid #27ba9b;
    color: #27ba9b;
}

.search {
    float: left;
    margin-left: 35px;
    border-bottom: 2px solid #e7e7e7;
}

.search::before {
    content: '';
    display: inline-block;
    vertical-align: middle;
    margin-top: -2px;
    width: 18px;
    height: 22px;
    background: url(.././images/sprites.png) no-repeat -79px -69px;
}

.search input {
    width: 172px;
    height: 30px;
    margin-top: 26px;
    padding-left: 30px;

}

.search input::placeholder {
    color: #ccc;
}

.car {
    position: relative;
    float: left;
    width: 23px;
    height: 23px;
    background-color: #27ba9b;
    margin-top: 30px;
    margin-left: 15px;
    background: url(.././images/sprites.png) no-repeat -119px -70px;
}

.car span {
    position: absolute;
    top: -8px;
    right: -10px;
    font-size: 13px;
    line-height: 14px;
    padding: 0 5px;
    background-color: #e26237;
    border-radius: 7px;
    color: #fff;
}

/* 主导航栏结束 */
/* 底部模块开始 */
footer {
    height: 645px;
}

.ft {
    height: 50%;
}

.ft dl {
    width: 25%;
    float: left;
    text-align: center;
    margin-top: 79px;
    color: #999;
}

.ft dl dt {
    margin-bottom: 30px;
}

.kefu dd,
.guanzhu dd {
    display: inline-block;
    vertical-align: middle;
    width: 92px;
    height: 92px;
    border: 1px solid #eee;
    margin-right: 5px;

}

.kefu dd a,
.guanzhu dd a {
    display: block;
    margin-top: 22px;
    width: 100%;
    height: 100%;
    font-size: 14px;
    color: #999;
}

.kefu dd a::before,
.guanzhu dd a::before {
    display: block;
    width: 35px;
    height: 31px;
    content: '';
    margin: 0 auto;
    padding-bottom: 5px;
    background: url(../images/sprites.png) no-repeat;
    transition: all .5s;
}

.kefu .zaixian a::before {
    background-position: -250px -70px;
}

.kefu .zaixian a:hover::before {
    background-position: -204px -70px;
}

.kefu .wenti a::before {
    background-position: -350px -70px;
}

.kefu .wenti a:hover::before {
    background-position: -300px -70px;
}

.guanzhu .gongzhong a::before {
    background-position: -250px -15px;
}

.guanzhu .gongzhong a:hover::before {
    background-position: -205px -15px;
}

.guanzhu .weibo a::before {
    background-position: -350px -15px;
}

.guanzhu .weibo a:hover::before {
    background-position: -300px -15px;
}



.xiazai dd {
    width: 105px;
    height: 103px;
    display: inline-block;

}

.xiazai dd a {
    color: #999;
    font-size: 14px;
}

.xiazai dd button {
    width: 107px;
    height: 32px;
    background-color: #27ba9b;
    color: #FFF;
    margin-top: 10PX;
    border: none;
    cursor: pointer;
}

.xiazai dd img {
    width: 100%;
    height: 100%;
    border: 1px solid #eee;
    padding: 5px;
    margin-top: -60px;
}

.ft .rexian dt {
    margin-bottom: 40px;
}

.rexian dd:nth-of-type(1) {
    font-size: 22px;
}

.rexian dd:nth-of-type(2) {
    font-size: 15px;
    margin-top: 5px;
}

.fb {
    height: 50%;
    background-color: #333;
}

.fb .fb_t {
    height: 170px;
    line-height: 175px;
    width: 1393px;
    margin: 0 auto 40px;
    border-bottom: 1px solid #434343;
    text-align: center;
}

.fb .fb_t ul li {
    float: left;
    width: 33.33%;
    color: #fff;
    font-size: 28px;
}

.fb .fb_t ul li::before {
    content: '';
    display: inline-block;
    vertical-align: middle;
    width: 58px;
    height: 58px;
    background: url(../images/sprites.png) no-repeat;
}

.fb .fb_t ul li:nth-child(2):before {
    background-position: -65px 0;
}

.fb .fb_t ul li:nth-child(2):before {
    background-position: -130px 0;
}

.fb .fb_b {
    text-align: center;
    color: #999;
}

.fb .fb_b i {
    padding: 0 4px;
}

.fb .fb_b p a {
    color: #999;
    font-size: 14px;

}

.fb .fb_b p a:hover {
    color: #27ba9b;
}

.fb .fb_b p:nth-child(2) {
    margin-top: 5px;
}

/* 底部模块结束 */

1.3.3 首页样式(login)样式

小兔鲜项目-原生JS实现页面登录模块_第4张图片

2、JavaScript源码

由于我们需要将登录时输入的用户名传到首页,所以我们需要用到本地存储技术,将用户名存储到浏览器中以便首页对其进行调用。

   <script>
        // 1、tab账户登录和二维码登录切换
        // 获取tab-nav元素
        const tab_nav = document.querySelector('.tab-nav')
        const tab_pane = document.querySelectorAll('.tab-pane')
        // 给tab-nav注册事件委托
        tab_nav.addEventListener('click', function (e) {
            // 判断是否点击的A链接
            if (e.target.tagName === 'A') {
                // 移除类
                tab_nav.querySelector('.active').classList.remove('active')
                // 添加类
                e.target.classList.add('active')
                // 切换下面的盒子
                // 移除所有盒子
                for (let i = 0; i < tab_pane.length; i++) {
                    tab_pane[i].style.display = 'none'
                }
                // 添加盒子
                tab_pane[e.target.dataset.id].style.display = 'block'
            }
        })
        // 2点击提交模块
        // 2.1获取整个form表单
        const form = document.querySelector('form')
        const remember = document.querySelector('.remember')
        const userName = document.querySelector('[name="username"]')
        // 2.2给form注册提交事件
        form.addEventListener('submit',function(e) {
            e.preventDefault()
            if(!remember.checked) {
                return alert('请勾选同意用户协议')
            }
            // 将用户名存入本地存储中,以便后续首页的调用
            localStorage.setItem('xtx_name',userName.value)
            // 跳转到首页
            location.href = './index.html'
        })
    </script>

2.1实现效果:

小兔鲜项目-原生JS实现页面登录模块_第5张图片

在这里插入图片描述
需要源文件请私信我免费领取

你可能感兴趣的:(综合案例,JS笔记,javascript,前端,html,css)