资讯专栏INFORMATION COLUMN

有趣的交互系列 - 文字遮罩入场效果

邹强 / 1909人阅读

摘要:在这个系列里分享一些简单,但是很有意思的交互效果附上地址和地址滚动,添加对应加载的比如巧用实现文字和遮罩层的动画效果文字动画遮罩层动画首先然后把且,这样就实现了遮罩层的进场和退出效果随机获取数组项

在这个系列里分享一些简单,但是很有意思的交互效果~
附上demo地址和github地址

滚动,添加对应加载的class(比如loaded)

巧用animation实现文字和遮罩层的动画效果

文字动画: opacity 0 -> 1;

遮罩层动画: 首先width 0 -> 100%,然后把width 100% -> 0left 0 -> 100%,这样就实现了遮罩层的进场和退出效果;

html:

</>复制代码

    • SANDWICHES & PANCAKE

    • GARDEN

    • MORNING & TOMORROW & FRIEND

    • ORANGE & BIRD & SHEEP & CUP & BUS

    • APPLE & FRUIT & CAR

    • CAKE & PICTURE & CAT & STAMP

    • PLANE & BOOK & RACKET & GLASS & BED

    • APPLE
      BANANA & PINE APPLE & SHEEP

    • BANANA & PINE APPLE

    • PUMPKIN & TARO & CARROT

    • HORSERADISH & LETTUCE

    • PUMPKIN & TARO & CARROT

    • HORSERADISH & LETTUCE

    • POTATO & BURDOCK

    • EGG & BAG & ROSE & CHAIR & BAT

    • FISH & NOTEBOOK & PENCIL & DOG & DESK

    • WATCH & MITT & MILK & FLOWER

    • DOOR & BOAT & PIANO & 

    • POTATO & BURDOCK

    • APPLE
      BANANA & PINE APPLE

    • LETTER
      CAP & TAPE & MAIL & BOX

    • APPLE
      BANANA & PINE APPLE

    • TURNIP & OKRA & PEA

    • SHIITAKE & BEEFSTEAK PLANT

css:

</>复制代码

  1. body {
  2. -webkit-font-smoothing: antialiased;
  3. -moz-osx-font-smoothing: grayscale;
  4. background-color: #000;
  5. }
  6. main {
  7. padding: 10vw 0;
  8. }
  9. ul {
  10. width: 100%;
  11. max-width: 70%;
  12. margin: 0 auto;
  13. }
  14. li {
  15. margin: 10vw 0;
  16. text-align: left;
  17. }
  18. p {
  19. display: block;
  20. color: #fff;
  21. font-family: "Lato", sans-serif;
  22. font-size: 2vw;
  23. font-weight: 900;
  24. line-height: 1.2;
  25. }
  26. p+p {
  27. margin-top: 10px;
  28. }
  29. li:first-child {
  30. margin-top: 0;
  31. }
  32. li:last-child {
  33. margin-bottom: 0;
  34. }
  35. li:nth-child(even) {
  36. text-align: right;
  37. }
  38. a {
  39. color: #fff;
  40. }
  41. a:hover {
  42. text-decoration: none;
  43. }
  44. [data-reveal="content"] {
  45. display: inline-block;
  46. position: relative;
  47. }
  48. [data-reveal="cover"] {
  49. position: absolute;
  50. top: 0;
  51. left: 0;
  52. width: 0;
  53. height: 100%;
  54. z-index: 1;
  55. }
  56. [data-reveal="text"] {
  57. opacity: 0;
  58. }
  59. @keyframes reveal-cover {
  60. 0% {
  61. width: 0;
  62. left: 0;
  63. }
  64. 44% {
  65. width: 100%;
  66. left: 0;
  67. }
  68. 54% {
  69. width: 100%;
  70. left: 0;
  71. }
  72. 100% {
  73. width: 0;
  74. left: 100%;
  75. }
  76. }
  77. @keyframes reveal-text {
  78. 0% {
  79. opacity: 0;
  80. }
  81. 44% {
  82. opacity: 0;
  83. }
  84. 54% {
  85. opacity: 1;
  86. }
  87. }
  88. [data-js="reveal"].loaded [data-reveal="cover"] {
  89. animation: reveal-cover 1.5s ease-in-out;
  90. }
  91. [data-js="reveal"].loaded [data-reveal="text"] {
  92. opacity: 1;
  93. animation: reveal-text 1.5s ease-in-out;
  94. }

javascript:

</>复制代码

  1. const COLOR_LIST = ["#7f00ff", "#ff00ff", "#0000ff", "#007fff", "#00ffff"];
  2. let $targetList;
  3. const init = () => {
  4. $targetList = document.querySelectorAll("[data-js="reveal"]");
  5. setup();
  6. window.addEventListener("scroll", onScroll, false);
  7. window.dispatchEvent(new Event("scroll"));
  8. }
  9. // 随机获取数组项
  10. const getArrayRandomValue = (array) => array[Math.floor(Math.random() * array.length)];
  11. const setup = () => {
  12. for (const $target of $targetList) {
  13. const content = $target.innerHTML;
  14. const color = "revealColor" in $target.dataset ? $target.dataset.revealColor : getArrayRandomValue(COLOR_LIST);
  15. $target.innerHTML = `
    ${content}
    `;
  16. }
  17. }
  18. const onScroll = () => {
  19. const windowH = window.innerHeight;
  20. const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  21. const isMostScroll = document.body.clientHeight <= scrollTop + windowH;
  22. for (const $target of $targetList) {
  23. if ($target.classList.contains("loaded")) continue;
  24. const rect = $target.getBoundingClientRect();
  25. const top = rect.top + scrollTop;
  26. if (isMostScroll || top <= scrollTop + (windowH * .8)) $target.classList.add("loaded");
  27. }
  28. }
  29. document.addEventListener("DOMContentLoaded", init, false);

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/53978.html

相关文章

  • 有趣交互系列 - 文字遮罩入场效果

    摘要:在这个系列里分享一些简单,但是很有意思的交互效果附上地址和地址滚动,添加对应加载的比如巧用实现文字和遮罩层的动画效果文字动画遮罩层动画首先然后把且,这样就实现了遮罩层的进场和退出效果随机获取数组项 在这个系列里分享一些简单,但是很有意思的交互效果~附上demo地址和github地址 showImg(https://makapicture.oss-cn-beijing.aliyuncs....

    tyheist 评论0 收藏0
  • 如何使用CSS和SVG剪切和遮罩技术

    摘要:通过结合使用和遮罩技术,你将会拥有更多的可能性去使用网络图像。在图像上应用遮罩元素为了使用得到一种感觉,我们将在图像上使用遮罩。浏览器支持在我们使用这种新的处理图像的方法之前,注意到浏览器对剪切和遮罩的支持不一致是非常重要的。 本文转载自:众成翻译译者:hidoos链接:http://www.zcfy.cc/article/1100原文:https://getflywheel.com/...

    hover_lew 评论0 收藏0
  • Codepen 每日精选(2018-4-19)

    摘要:按下右侧的点击预览按钮可以在当前页面预览,点击链接可以打开原始页面。 按下右侧的点击预览按钮可以在当前页面预览,点击链接可以打开原始页面。 纯 css 写的创意灯泡动画https://codepen.io/uzcho_/ful... 纯 css 画的文字阴影https://codepen.io/GeorgePark... 纯 css 画的海报https://codepen.io/vic...

    CNZPH 评论0 收藏0
  • Codepen 每日精选(2018-4-19)

    摘要:按下右侧的点击预览按钮可以在当前页面预览,点击链接可以打开原始页面。 按下右侧的点击预览按钮可以在当前页面预览,点击链接可以打开原始页面。 纯 css 写的创意灯泡动画https://codepen.io/uzcho_/ful... 纯 css 画的文字阴影https://codepen.io/GeorgePark... 纯 css 画的海报https://codepen.io/vic...

    yagami 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<