爬行的蜗牛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: golang Linux PHP
查看: 63|回复: 0

轻松实现卡片悬停效果的 8 个实用代码片段

[复制链接]

94

主题

36

回帖

849

积分

管理员

积分
849
发表于 2024-11-5 22:10:12 | 显示全部楼层 |阅读模式
6401.gif
卡片 UI 布局因其多功能性而在网页设计中大受欢迎,适用于展示各种内容。为卡片添加悬停效果不仅能增强视觉吸引力,还能提升用户体验。以下是一些用 CSS 和 JavaScript 实现的优秀卡片悬停效果示例,供你参考。

有时候,一次性展示大量内容可能会让用户感到不知所措。这款卡片交互效果就能很好地解决这个问题。卡片初始状态下只有一个简单的标题和背景图片,当你将鼠标悬停在卡片上时,会出现描述性文字和号召性用语。这种效果是通过纯 CSS 实现的,非常简洁。


HTML
  1. - var cards = [{ title: 'Mountain View', copy: 'Check out all of these gorgeous mountain trips with beautiful views of, you guessed it, the mountains', button: 'View Trips' }, { title: 'To The Beach', copy: 'Plan your next beach trip with these fabulous destinations', button: 'View Trips' }, { title: 'Desert Destinations', copy: 'It\'s the desert you\'ve always dreamed of', button: 'Book Now' }, { title: 'Explore The Galaxy', copy: 'Seriously, straight up, just blast off into outer space today', button: 'Book Now' }]


  2. mixin card(title, copy, button)
  3.   .card
  4.     .content
  5.       h2.title= title
  6.       p.copy= copy
  7.       button.btn= button

  8. main.page-content
  9.   each card in cards
  10.     +card(card.title, card.copy, card.button)
复制代码
CSS
  1. @import url('https://fonts.googleapis.com/css?family=Cardo:400i|Rubik:400,700&display=swap');

  2. $imageIds: '1517021897933-0e0319cfbc28', '1533903345306-15d1c30952de', '1545243424-0ce743321e11', '1531306728370-e2ebd9d7bb99';

  3. $bp-md: 600px;
  4. $bp-lg: 800px;

  5. :root {
  6.   --d: 700ms;
  7.   --e: cubic-bezier(0.19, 1, 0.22, 1);
  8.   --font-sans: 'Rubik', sans-serif;
  9.   --font-serif: 'Cardo', serif;
  10. }

  11. * {
  12.   box-sizing: border-box;
  13. }

  14. html, body {
  15.   height: 100%;
  16. }

  17. body {
  18.   display: grid;
  19.   place-items: center;
  20. }

  21. .page-content {
  22.   display: grid;
  23.   grid-gap: 1rem;
  24.   padding: 1rem;
  25.   max-width: 1024px;
  26.   margin: 0 auto;
  27.   font-family: var(--font-sans);
  28.   
  29.   @media (min-width: $bp-md) {
  30.     grid-template-columns: repeat(2, 1fr);
  31.   }
  32.   
  33.   @media (min-width: $bp-lg) {
  34.     grid-template-columns: repeat(4, 1fr);
  35.   }
  36. }



  37. .card {  
  38.   position: relative;
  39.   display: flex;
  40.   align-items: flex-end;
  41.   overflow: hidden;
  42.   padding: 1rem;
  43.   width: 100%;
  44.   text-align: center;
  45.   color: whitesmoke;
  46.   background-color: whitesmoke;
  47.   box-shadow: 0 1px 1px rgba(0,0,0,0.1),
  48.     0 2px 2px rgba(0,0,0,0.1),
  49.     0 4px 4px rgba(0,0,0,0.1),
  50.     0 8px 8px rgba(0,0,0,0.1),
  51.     0 16px 16px rgba(0,0,0,0.1);
  52.   
  53.   @media (min-width: $bp-md) {
  54.     height: 350px;
  55.   }
  56.   
  57.   &:before {
  58.     content: '';
  59.     position: absolute;
  60.     top: 0;
  61.     left: 0;
  62.     width: 100%;
  63.     height: 110%;
  64.     background-size: cover;
  65.     background-position: 0 0;
  66.     transition: transform calc(var(--d) * 1.5) var(--e);
  67.     pointer-events: none;
  68.   }
  69.   
  70.   &:after {
  71.     content: '';
  72.     display: block;
  73.     position: absolute;
  74.     top: 0;
  75.     left: 0;
  76.     width: 100%;
  77.     height: 200%;
  78.     pointer-events: none;
  79.     background-image: linear-gradient(
  80.       to bottom,
  81.       hsla(0, 0%, 0%, 0) 0%,
  82.       hsla(0, 0%, 0%, 0.009) 11.7%,
  83.       hsla(0, 0%, 0%, 0.034) 22.1%,
  84.       hsla(0, 0%, 0%, 0.072) 31.2%,
  85.       hsla(0, 0%, 0%, 0.123) 39.4%,
  86.       hsla(0, 0%, 0%, 0.182) 46.6%,
  87.       hsla(0, 0%, 0%, 0.249) 53.1%,
  88.       hsla(0, 0%, 0%, 0.320) 58.9%,
  89.       hsla(0, 0%, 0%, 0.394) 64.3%,
  90.       hsla(0, 0%, 0%, 0.468) 69.3%,
  91.       hsla(0, 0%, 0%, 0.540) 74.1%,
  92.       hsla(0, 0%, 0%, 0.607) 78.8%,
  93.       hsla(0, 0%, 0%, 0.668) 83.6%,
  94.       hsla(0, 0%, 0%, 0.721) 88.7%,
  95.       hsla(0, 0%, 0%, 0.762) 94.1%,
  96.       hsla(0, 0%, 0%, 0.790) 100%
  97.     );
  98.     transform: translateY(-50%);
  99.     transition: transform calc(var(--d) * 2) var(--e);
  100.   }
  101.   
  102.   @each $id in $imageIds {
  103.     $i: index($imageIds, $id);
  104.    
  105.     &:nth-child(#{$i}):before {
  106.       background-image: url(https://images.unsplash.com/photo-#{$id}?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ);
  107.     }
  108.   }
  109. }

  110. .content {
  111.   position: relative;
  112.   display: flex;
  113.   flex-direction: column;
  114.   align-items: center;
  115.   width: 100%;
  116.   padding: 1rem;
  117.   transition: transform var(--d) var(--e);
  118.   z-index: 1;
  119.   
  120.   > * + * {
  121.     margin-top: 1rem;
  122.   }
  123. }

  124. .title {
  125.   font-size: 1.3rem;
  126.   font-weight: bold;
  127.   line-height: 1.2;
  128. }

  129. .copy {
  130.   font-family: var(--font-serif);
  131.   font-size: 1.125rem;
  132.   font-style: italic;
  133.   line-height: 1.35;
  134. }

  135. .btn {
  136.   cursor: pointer;
  137.   margin-top: 1.5rem;
  138.   padding: 0.75rem 1.5rem;
  139.   font-size: 0.65rem;
  140.   font-weight: bold;
  141.   letter-spacing: 0.025rem;
  142.   text-transform: uppercase;
  143.   color: white;
  144.   background-color: black;
  145.   border: none;
  146.   
  147.   &:hover {
  148.     background-color: lighten(black, 5%);
  149.   }
  150.   
  151.   &:focus {
  152.     outline: 1px dashed yellow;
  153.     outline-offset: 3px;
  154.   }
  155. }

  156. @media (hover: hover) and (min-width: $bp-md) {
  157.   .card:after {
  158.     transform: translateY(0);
  159.   }
  160.   
  161.   .content {
  162.     transform: translateY(calc(100% - 4.5rem));
  163.    
  164.     > *:not(.title) {
  165.       opacity: 0;
  166.       transform: translateY(1rem);
  167.       transition:
  168.         transform var(--d) var(--e),
  169.         opacity var(--d) var(--e);
  170.     }
  171.   }
  172.   
  173.   .card:hover,
  174.   .card:focus-within {
  175.     align-items: center;

  176.     &:before { transform: translateY(-4%); }
  177.     &:after { transform: translateY(-50%); }

  178.     .content {
  179.       transform: translateY(0);

  180.       > *:not(.title) {
  181.         opacity: 1;
  182.         transform: translateY(0);
  183.         transition-delay: calc(var(--d) / 8);
  184.       }
  185.     }
  186.   }
  187.   
  188.   .card:focus-within {
  189.     &:before,
  190.     &:after,
  191.     .content,
  192.     .content > *:not(.title) {
  193.       transition-duration: 0s;
  194.     }
  195.   }
  196. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表