50个前端实战项目之07:拆分登录页面

大家好,我是宝哥。

今天讲50个前端实战项目之07:拆分登录页面。

源码下载地址

https://github.com/bradtraversy/50projects50days/tree/master/split-landing-page

前端实战项目系列正在更新:07/50

项目介绍

本项目演示了一个拆分式的页面,分左右两部分,分别展示不同的内容及其购买按钮。鼠标悬停在左侧或右侧时,对应的内容会放大,另一侧则会缩小,营造一种互斥互动的切换效果。

效果预览

df8d2563210fc84fe486e56d5d1d91b2.png

在线预览(文末点击原文链接可直达):

https://qdkfweb.cn/50projects50days/split-landing-page/

核心代码

HTML

  • 包含一个作为容器的 .container 元素,其中又嵌套左右两侧的登录内容 .split.left.split.right

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Split Landing Page
    </title>
  </head>
  <body>
    <div class="container">
      <div class="split left">
        <h1>Playstation 5</h1>
        <a href="#" class="btn">Buy Now</a>
      </div>
      <div class="split right">
        <h1>XBox Series X</h1>
        <a href="#" class="btn">Buy Now</a>
      </div>
    </div>

    <script src="script.js"></script>
  </body>
</html>

CSS

  • 定义了整体的布局、背景图片、按钮样式以及悬停时的切换效果。

  • 关键在于利用伪元素 (::before) 在既有背景图片之上覆盖一层半透明的背景色,并控制其颜色和透明度。

  • 左右两侧的登录内容 .split.left.split.right 和其对应的伪元素都设置了 transition 属性,以便在鼠标悬停时平滑地切换样式。

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');

:root {
  --left-bg-color: rgba(87, 84, 236, 0.7);
  --right-bg-color: rgba(43, 43, 43, 0.8);
  --left-btn-hover-color: rgba(87, 84, 236, 1);
  --right-btn-hover-color: rgba(28, 122, 28, 1);
  --hover-width: 75%;
  --other-width: 25%;
  --speed: 1000ms;
}

* {
  box-sizing: border-box;
}

body {
  font-family: 'Roboto', sans-serif;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}

h1 {
  font-size: 4rem;
  color: #fff;
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translateX(-50%);
  white-space: nowrap;
}

.btn {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  left: 50%;
  top: 40%;
  transform: translateX(-50%);
  text-decoration: none;
  color: #fff;
  border: #fff solid 0.2rem;
  font-size: 1rem;
  font-weight: bold;
  text-transform: uppercase;
  width: 15rem;
  padding: 1.5rem;
}

.split.left .btn:hover {
  background-color: var(--left-btn-hover-color);
  border-color: var(--left-btn-hover-color);
}

.split.right .btn:hover {
  background-color: var(--right-btn-hover-color);
  border-color: var(--right-btn-hover-color);
}

.container {
  position: relative;
  width: 100%;
  height: 100%;
  background: #333;
}

.split {
  position: absolute;
  width: 50%;
  height: 100%;
  overflow: hidden;
}

.split.left {
  left: 0;
  background: url('ps.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}

.split.left::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: var(--left-bg-color);
}

.split.right {
  right: 0;
  background: url('xbox.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}

.split.right::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: var(--right-bg-color);
}

.split.right,
.split.left,
.split.right::before,
.split.left::before {
  transition: all var(--speed) ease-in-out;
}

.hover-left .left {
  width: var(--hover-width);
}

.hover-left .right {
  width: var(--other-width);
}

.hover-right .right {
  width: var(--hover-width);
}

.hover-right .left {
  width: var(--other-width);
}

@media (max-width: 800px) {
  h1 {
    font-size: 2rem;
    top: 30%;
  }

  .btn {
    padding: 1.2rem;
    width: 12rem;
  }
}

JavaScript

  • 获取左右两侧登录内容的 DOM 节点 (leftright) 和容器 (container) 的 DOM 节点。

  • 为左右两侧的内容分别添加鼠标移入/移出事件的监听器。

  • 事件处理函数中,通过为容器添加/移除 CSS 类 hover-lefthover-right 来控制左右两侧内容的宽度变化,实现切换效果。

const left = document.querySelector('.left')
const right = document.querySelector('.right')
const container = document.querySelector('.container')

left.addEventListener('mouseenter', () => container.classList.add('hover-left'))
left.addEventListener('mouseleave', () => container.classList.remove('hover-left'))

right.addEventListener('mouseenter', () => container.classList.add('hover-right'))
right.addEventListener('mouseleave', () => container.classList.remove('hover-right'))

总结

该项目利用 CSS 的伪元素 (::before) 和 transition 属性实现背景色的覆盖和切换动画。JavaScript 则通过监听鼠标移入/移出事件,控制容器的 CSS 类,从而触发样式的切换。

动图全过程展示:

345c50f42b01051420b99e1430b80e51.gif

在线预览(点底部原文链接可直达):https://qdkfweb.cn/50projects50days/split-landing-page/

最后

如果你觉得宝哥今天的尝试对你有帮助,就给我点个赞,关注一波。分享出去,也许你的转发能给别人带来一点启发。

以后我也会多尝试共读其它项目,如果看到喜欢的项目也可以留言告诉我,今天的教程你学会了吗?学会了,就在评论区刷一个,学会了。

欢迎长按图片加好友,宝哥会第一时间和你分享前端行业趋势,面试资源,学习途径等等。

ba492bc42bb9d057df5afc9e8274c9fd.png

添加好友备注【加群】拉你进技术交流群

公众号前端开发博客 专注 前端开发技术,分享 前端开发资源WEB前沿资讯,如果喜欢我的分享,给 宝哥 点一个 或者 分享 都是对我的支持

关注公众号后,在首页:

  • 回复「小抄」,领取Vue、JavaScript 和 WebComponent 小抄 PDF

  • 回复「Vue脑图」获取 Vue 相关脑图

  • 回复「思维图」获取 JavaScript 相关思维图

  • 回复「简历」获取简历制作建议

  • 回复「简历模板」获取精选的简历模板

  • 回复「电子书」下载我整理的大量前端资源,含面试、Vue实战项目、CSS和JavaScript电子书等。

  • 回复「知识点」下载高清JavaScript知识点图谱

  • 回复「读书」下载成长的相关电子书

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值