純CSS實現映像滑鼠移至上方效果

來源:互聯網
上載者:User

今天來看一組純CSS實現的滑鼠移至上方效果,線上研究代碼點效果一、效果二、效果三,下載收藏點這裡,效果預覽點這裡。

效果1效果1的html檔案

<div class="pic"><img src="pic.jpg" alt=""><span>這裡是個標題呀</span></div>
css檔案中我們主要實現定位和hover動畫。

/* 圖片盒子的樣式,寬高、邊框、陰影、位置、滑鼠樣式、溢出 */.pic{width: 500px;height: 500px;border: 10px solid #333;box-shadow: 0 0 10px rgba(0,0,0,.6);margin: 50px auto;position: relative;overflow: hidden;cursor: pointer;}/* 圖片標題的樣式,寬高、背景色、水平垂直置中、定位,透明度 */.pic span{position: absolute;left: 0;bottom: 0;width: 500px;height: 50px;background: rgba(0,0,0,.5);color: #fff;font-size: 24px;line-height: 50px;text-align: center;opacity: 0;}/* 給需要動畫的元素加過渡屬性 */.pic img,.pic span{transition: all .5s;}/* hover之後透明動畫 */.pic:hover span{opacity: 1;}/* hover之後圖片放大的效果 */.pic:hover img{transform: scale(2);}
效果2

下面看效果2,上下滑動開啟的實現過程。首先看html檔案,我們需要兩個圖片元素,一個顯示上半部分,一個顯示下半部分。

<div class="pic"><img class="top" src="pic.jpg" alt="top"><img class="bottom" src="pic.jpg" alt="bottom"><span>OK,You can see it.</span></div>
css檔案如下,我們把實現原理寫到注釋裡,就不一一解釋了。

/* 映像盒子的樣式設定,寬高、邊框、陰影、溢出、位置 */div.pic{width: 500px;height: 500px;cursor: pointer;margin: 50px auto;position: relative;overflow: hidden;border: 10px solid #333;box-shadow: 0 0 10px rgba(0,0,0,.8);}/* 上下兩半部分的統一設定,絕對位置和過渡 */div.pic img{position: absolute;left: 0;transition: all 1s;}/* 上半部分映像的裁剪和定位 */div.pic img.top{top: 0;clip: rect(0px,500px,250px,0px);}/* 下半部分映像的裁剪和定位 */div.pic img.bottom{bottom: 0;clip: rect(250px,500px,500px,0px);}/* hover之後映像的定位改變 */div.pic:hover img.top{top: -50px;}div.pic:hover img.bottom{bottom: -50px;}/* 映像標題的樣式設定,文字顏色、水平置中、垂直置中 */div.pic span{display: inline-block;;width: 100%;text-align: center;line-height: 500px;font-size: 24px;}
重點解釋下CSS Clip的裁剪原理。

clip: rect(<top>, <right>, <bottom>, <left>);
這裡的top、right、bottom、left指的是裁剪之後的矩形的上右下左四個邊到原始邊的距離,如所示


大家可以看看張鑫旭大牛的《CSS clip:rect矩形剪裁功能及一些應用介紹》瞭解詳情,或者通過線上體會案例體驗裁剪效果。

大家也可以利用Codrops上的《Expanding Overlay Effect》學習clip屬性的超酷應用。

效果3效果3的痛點在於三角線映像的實現,我們這裡主要利用skew()實現。html檔案是這樣的,我們需要分別在兩個映像上加容器,然後分別對映像容器和映像設定相反方向的斜切。
<div class="pic">    <div class="top">        <img src="http://gx.zptc.cn/whqet/img/road.jpg">    </div>    <div class="bottom">        <img class="bottom" src="http://gx.zptc.cn/whqet/img/road.jpg">    </div>      <span>OK,You can see it.</span></div>
效果css檔案
/* 映像盒子的樣式,與前兩個效果無異 */.pic {    width: 500px;    height: 500px;    overflow: hidden;    cursor: pointer;    border: 10px solid #333;    box-shadow: 0 0 10px rgba(0, 0, 0, .8);    margin: 50px auto;    position: relative;}/* 兩個映像的小容器的定位、斜切、溢出,因為映像寬高相同,所以斜切45度 */.pic .top, .pic .bottom {    width: 500px;    height: 500px;    overflow: hidden;    position: absolute;    transform: skew(-45deg);}/* 上下兩個映像小容器分別設定不同的位置,變換原點 */.pic .top {    left: 0;    top: 0;    transform-origin: 100% 0;    transition:all 1s, transform 0s;}.pic .bottom {    right: 0;    bottom: 0;    transform-origin: 0 100%;    transition:all 1s, transform 0s;}/* 上下兩個映像向相反方向斜切 */.pic .top img, .pic .bottom img {    width: 100%;    height: 100%;    transform-origin: inherit;    transform: skew(45deg);}/* hover之後映像小容器的變化 */.pic:hover .top {    left: -20px;    top: -20px;}.pic:hover .bottom {    right: -20px;    bottom: -20px;}/* 映像標題的設定 */.pic span {    transform: rotate(-45deg);    transform-origin: 0 100%;    position: absolute;    font-size:20px;    bottom:-.5em;    opacity: 0;    transition: all 1s ease-in .2s;}.pic:hover span {    opacity: 1;    text-indent: 15em;}

實現原理如所示,藍色線框代表映像盒子.top,我們分別給映像和映像盒子不同方向相同角度的斜切實現效果,只有線框內的映像顯示。


這樣我們可以實現映像的三角形裁切效果,根據這樣的原理大家可以嘗試不同形狀的實現。歡迎大家提供不同的實現思路。

That's all. 迎接鼓勵,歡迎拍磚。

---------------------------------------------------------------

前端開發whqet,關注web前端開發技術,分享網頁相關資源。
---------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.