1. 程式人生 > >CSS3動畫實現高亮光弧效果,文字和圖片(一閃而過)

CSS3動畫實現高亮光弧效果,文字和圖片(一閃而過)

前言

  好久沒有寫部落格啦,高亮文字和圖片一閃而過的特效,用CSS3來寫 

先看文字吧,

就上程式碼了

.shadow {
  /* 背景顏色線性漸變 */
  /* 老式寫法 */
  /* linear為線性漸變,也可以用下面的那種寫法。left top,right top指的是漸變方向,左上到右上 */
  /* color-stop函式,第一個表示漸變的位置,0為起點,0.5為中點,1為結束點;第二個表示該點的顏色。所以本次漸變為兩邊灰色,中間漸白色 */
  background: -webkit-gradient(
    linear,
    left top,
    right top,
    color-stop(0, #4d4d4d),
    color-stop(0.4, #4d4d4d),
    color-stop(0.5, white),
    color-stop(0.6, #4d4d4d),
    color-stop(1, #4d4d4d)
  )
; /* 設定為text,意思是把文字內容之外的背景給裁剪掉 */ -webkit-background-clip: text; /* 設定物件中的文字填充顏色 這裡設定為透明 */ -webkit-text-fill-color: transparent; /* 每隔2秒呼叫下面的CSS3動畫 infinite屬性為迴圈執行animate */ -webkit-animation: animate 1.5s infinite; animation: animate 1.5s infinite; } @keyframes animate { from { background-position
: -100px; } to { background-position: 100px; } }
  <span class="shadow">2019你好</span>

圖片呢:別急

 

 

<div class="img"><img src="https://cdndaily.quickbass.com/o2o/merchant/3/1811261526335TMQpr9Y!r750x380">
        <div class="rolled"></div>
</div
>

介紹2種吧:

1種是無限迴圈

.img {
            display: block;
            position: relative;
            width: 800px;
            height: 450px;
            margin: 0 auto;
        }

        body {
            margin: 0;
            padding: 0;
        }

        .img:hover .rolled {
            -webkit-animation-play-state: paused;
            -moz-animation-play-state: paused;
            -o-animation-play-state: paused;
            -ms-animation-play-state: paused;
        }

        .rolled {
            position: absolute;
            top: 0;
            width: 80px;
            height: 500px;
            background: -moz-linear-gradient(left, rgba(255, 255, 255, 0)0, rgba(255, 255, 255, .2)50%, rgba(255, 255, 255, 0)100%);
            background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, .2)), color-stop(100%, rgba(255, 255, 255, 0)));
            background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0)0, rgba(255, 255, 255, .2)50%, rgba(255, 255, 255, 0)100%);
            background: -o-linear-gradient(left, rgba(255, 255, 255, 0)0, rgba(255, 255, 255, .2)50%, rgba(255, 255, 255, 0)100%);
            -webkit-transform: skewX(-25deg);
            -moz-transform: skewX(-25deg);
            -webkit-animation: rolled 2.5s .2s ease both infinite;
            -moz-animation: rolled 2.5s .2s ease both infinite;
            -o-animation: rolled 2.5s .2s ease both infinite;
            -ms-animation: rolled 2.5s .2s ease both infinite;
            overflow: hidden;
        }

        @-webkit-keyframes rolled {
            0% {
                left: -150px;
            }

            100% {
                left: 920px;
            }
        }

        @-moz-keyframes rolled {
            0% {
                left: -150px;
            }

            100% {
                left: 920px;
            }
        }

        @-o-keyframes rolled {
            0% {
                left: -150px;
            }

            100% {
                left: 920px;
            }
        }

        @-ms-keyframes rolled {
            0% {
                left: -150px;
            }

            100% {
                left: 920px;
            }
        }

        @keyframes rolled {
            0% {
                left: -150px;
            }

            100% {
                left: 920px;
            }
        }

2種是就 執行一次 

.img:before { 
content: ""; position: absolute; width:200px; height: 100%; top: 0; left: -150px; overflow: hidden;

background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255,255,255,0)), color-stop(50%, rgba(255,255,255,.2)), color-stop(100%, rgba(255,255,255,0)));/*老式語法書寫規則*/

background: -moz-linear-gradient(left, rgba(255,255,255,0)0, rgba(255,255,255,.2)50%, rgba(255,255,255,0)100%);
background: -webkit-linear-gradient(left, rgba(255,255,255,0)0, rgba(255,255,255,.2)50%, rgba(255,255,255,0)100%);
background: -o-linear-gradient(left, rgba(255,255,255,0)0, rgba(255,255,255,.2)50%, rgba(255,255,255,0)100%);

-webkit-transform: skewX(-25deg);
-moz-transform: skewX(-25deg)

}
.img:hover:before { left: 150%; transition: left 1s ease 0s; }