满足常用动画化元素需求的简短 CSS 代码片段

CSS 片段集合包含了 CSS3 的实用工具和交互示例。它包括用于创建常用布局、样式化和动画化元素的现代技术,以及处理用户交互的片段。

满足常用动画化元素需求的简短 CSS 代码片段

示例

创建一张在悬停时移动的卡片


  • perspective 在元素上设置适当的值.container 以允许移动效果
  • transition 为元素 transform 的属性添加一个.card
  • 用于 Document.querySelector()选择.card 元素并为 mousemovemouseout 事件添加事件侦听器
  • 用于 Element.getBoundingClientRect()获取元素的 xywidthheight.card
  • 将相对距离计算为-1 和轴 1 之间的值,并通过属性应用xytransform

满足常用动画化元素需求的简短 CSS 代码片段

HTML

<div class="container">
  <div class="shifting-card">
    <div class="content">
      <h3>Card</h3>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Corrupti repellat, consequuntur doloribus voluptate esse iure?</p>
    </div>
  </div>
</div>

CSS

.container {
  display: flex;
  padding: 24px;
  justify-content: center;
  align-items: center;
  background: #f3f1fe;
}

.shifting-card {
  width: 350px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #fff;
  border-radius: 10px;
  margin: 0.5rem;
  transition: transform 0.2s ease-out;
  box-shadow: 0 0 5px -2px rgba(0, 0, 0, 0.1);
}

.shifting-card .content {
  text-align: center;
  margin: 2rem;
  line-height: 1.5;
  color: #101010;
}

JS

const card = document.querySelector('.shifting-card');
const { x, y, width, height } = card.getBoundingClientRect();
const cx = x + width / 2;
const cy = y + height / 2;

const handleMove = e => {
  const { pageX, pageY } = e;
  const dx = (cx - pageX) / (width / 2);
  const dy = (cy - pageY) / (height / 2);
  e.target.style.transform =
    `rotateX(${10 * dy * -1}deg) rotateY(${10 * dx}deg)`;
};

const handleOut = e => {
  e.target.style.transform = 'initial';
};

card.addEventListener('mousemove', handleMove);
card.addEventListener('mouseout', handleOut);


在悬停时显示图像叠加效果


  • 使用伪元素::before::after 来创建覆盖层 overlay 的顶部和底部条。通过设置它们的透明度 opacity变换 transform过渡 transition 效果,来实现所需的效果。
  • <figcaption> 用于叠加层的文本。设置 display: flex,flex-direction: columnjustify-content: center 将文本居中放置在图像中
  • 使用:hover 伪选择器更新所有元素的 opacitytransform 并显示叠加层

满足常用动画化元素需求的简短 CSS 代码片段

HTML

<figure class="hover-img">
  <img src="https://picsum.photos/id/200/440/320.jpg"/>
  <figcaption>
    <h3>Lorem <br/>Ipsum</h3>
  </figcaption>
</figure>

CSS

.hover-img {
  background-color: #000;
  color: #fff;
  display: inline-block;
  margin: 8px;
  max-width: 320px;
  min-width: 240px;
  overflow: hidden;
  position: relative;
  text-align: center;
  width: 100%;
}

.hover-img * {
  box-sizing: border-box;
  transition: all 0.45s ease;
}

.hover-img::before,
.hover-img::after {
  background-color: rgba(0, 0, 0, 0.5);
  border-top: 32px solid rgba(0, 0, 0, 0.5);
  border-bottom: 32px solid rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  content: '';
  transition: all 0.3s ease;
  z-index: 1;
  opacity: 0;
  transform: scaleY(2);
}

.hover-img img {
  vertical-align: top;
  max-width: 100%;
  backface-visibility: hidden;
}

.hover-img figcaption {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  align-items: center;
  z-index: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  line-height: 1.1em;
  opacity: 0;
  z-index: 2;
  transition-delay: 0.1s;
  font-size: 24px;
  font-family: sans-serif;
  font-weight: 400;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.hover-img:hover::before,
.hover-img:hover::after {
  transform: scale(1);
  opacity: 1;
}

.hover-img:hover > img {
  opacity: 0.7;
}

.hover-img:hover figcaption {
  opacity: 1;
}


图像悬停菜单


  • 使用<figure> 标签将 <img> 元素包裹起来,并使用 <div> 元素来包含菜单链接
  • 使用 opacityright 属性在悬停时为图像设置动画,创建滑动效果
  • <div> 元素的 left 属性设置为元素宽度的负值。当鼠标悬停在父元素上时,将其重置为 0,以便将菜单滑入
  • 使用 display: flex,flex-direction: columnjustify-content: center<div>上,使菜单项垂直居中

满足常用动画化元素需求的简短 CSS 代码片段

HTML

<figure class="hover-menu">
 <img src="https://picsum.photos/id/1060/800/480.jpg"/>
 <div>
  <a href="#">Home</a>
  <a href="#">Pricing</a>
  <a href="#">About</a>
 </div>
</figure>

CSS

.hover-menu {
  position: relative;
  overflow: hidden;
  margin: 8px;
  min-width: 340px;
  max-width: 480px;
  max-height: 290px;
  width: 100%;
  background: #000;
  text-align: center;
  box-sizing: border-box;
}

.hover-menu * {
  box-sizing: border-box;
}

.hover-menu img {
  position: relative;
  max-width: 100%;
  top: 0;
  right: 0;
  opacity: 1;
  transition: 0.3s ease-in-out;
}

.hover-menu div {
  position: absolute;
  top: 0;
  left: -120px;
  width: 120px;
  height: 100%;
  padding: 8px 4px;
  background: #000;
  transition: 0.3s ease-in-out;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.hover-menu div a {
  display: block;
  line-height: 2;
  color: #fff;
  text-decoration: none;
  opacity: 0.8;
  padding: 5px 15px;
  position: relative;
  transition: 0.3s ease-in-out;
}

.hover-menu div a:hover {
  text-decoration: underline;
}

.hover-menu:hover img {
  opacity: 0.5;
  right: -120px;
}

.hover-menu:hover div {
  left: 0;
  opacity: 1;
}


图像在悬停时旋转


  • 将鼠标悬停在父元素 <figure>上时使用 scale()rotate()transition 属性来为图片添加动画效果
  • 使用 overflow: hidden 在父元素上以隐藏图像转换中的多余部分

满足常用动画化元素需求的简短 CSS 代码片段

HTML

<figure class="hover-rotate">
  <img src="https://picsum.photos/id/669/600/800.jpg"/>
</figure>

CSS

.hover-rotate {
  overflow: hidden;
  margin: 8px;
  min-width: 240px;
  max-width: 320px;
  width: 100%;
}

.hover-rotate img {
  transition: all 0.3s;
  box-sizing: border-box;
  max-width: 100%;
}

.hover-rotate:hover img {
  transform: scale(1.3) rotate(5deg);
}

传送门

开源协议:CC-BY-4.0

开源地址:https://Github.com/30-seconds/30-seconds-of-code

项目合集:https://github.com/OpenTechCol/OpenTechCol

「回复【加群】加入开源技术交流群,干货很多!」

满足常用动画化元素需求的简短 CSS 代码片段-END-


原文始发于微信公众号(开源技术专栏):满足常用动画化元素需求的简短 CSS 代码片段

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/155436.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!