侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

浅谈CSS3鼠标移入图片动态提示效果(transform)

前端  /  管理员 发布于 7年前   285

第一次尝试着写博客,不好或者有误的地方希望大家多多指正呐,今天主要写的是关于CSS3的一个重要属性transform的一些用法,这些例子是之前在慕课网上学习某位老师的课程后自己敲的。

一、前言

1. transform是什么?

transform的含义是:改变,使....变形;转换

2. transform的常见属性有哪些?

transform的属性包括: translate()/rotate() / skew() / scale() /,分别还有x、y之分,比如:rotatex() 和 rotatey() ,以此类推。

transform:translate()

含义:变动,位移;例如向右位移20像素,向上位移50像素(向左向下为负值) 实例如下

.test01{-webkit-transform:translate(20px,50px);-moz-transform:translate(20px,50px)}

transform:rotate()

含义:旋转;“deg”是表示旋转的度数 例如“180deg”表示旋转“180度”  实例如下

.test02{-webkit-transform:rotate(180deg);-moz-transform:rotate(180deg)}

transform:skew()

含义:倾斜  实例如下

.test03{-webkit-transform:skew(20deg);-moz-transform:skew(20deg)} 

transform:scale()

含义:比例  1.8表示以1.8的比例放大 如果是放大整数倍如放大3倍 必须写成3.0  实例如下

.test03{-webkit-transform:scale(2.5);-moz-transform:scale(2.5)}

3. transform的实例

demo01 说明:鼠标移入后 图片左移 内容依次进入

步骤:

1.写好html代码并通过css设置好内容和图片的初始样式(文字内容都在图片上);

2.将描述内容通过transform属性位移到左侧 看不到为止(transform:translate(-600px,0););

3.接下来设置鼠标移入时(:hover)的样式  同样是利用transform   使内容左移的距离为0(transform:translate(0,0))这里用到transition-delay属性主要是为了让三个内容分别延迟不同的时间 形成依次进入的效果。  

/*图片左移 文字依次进入*/.test1{background: #fff;}.test1 figcaption p{background: #fff;color:#333;margin:5px 0;transform: translate(-600px,0px);}.test1 figcaption{padding:20px}.test1:hover figcaption p{transform: translate(0,0);}.test1 figcaption p:nth-of-type(1){transition-delay: 0.2s;}.test1 figcaption p:nth-of-type(2){transition-delay: 0.3s;}.test1 figcaption p:nth-of-type(3){transition-delay: 0.4s;}.test1:hover img{transform: translate(-5px,0);}
        <!--移动-->        <figure class="test1"><img src="img/altimg05.jpg"><figcaption>    <h2>图片标题</h2>    <p>这里是图片的相关描述内容</p>    <p>这里是图片的相关描述内容</p>    <p>这里是图片的相关描述内容</p></figcaption>        </figure>

demo02 说明:鼠标移入后 图片变模糊 矩形从图片外旋转进入图片中指定位置 文字从右侧飞过来 并逐渐显示

    

步骤:

1.写好html代码并通过css设置好内容和图片的初始样式(矩形文字都在图片上);

2.将矩形通过transform属性位移到上方 看不到为止 并设置旋转的角度为0  transform: translate(0,-400px) rotate(0deg);

3.接下来设置鼠标移入时(:hover)的样式 位移设置为0并旋转360度  transform: translate(0,0) rotate(360deg);

/*旋转*/.test2{background: #ccc;}.test2 figcaption{width: 100%;height: 100%;}.test2 figcaption h2{margin:15% 0 0 15%}.test2 figcaption p{margin-left:15%;transform: translate(50px,0);opacity: 0;}.test2 figcaption div{border:2px solid #ccc;width: 80%;height: 80%;position:absolute;top:10%;left:10%;transform: translate(0,-400px) rotate(0deg);}.test2:hover figcaption div{transform: translate(0,0) rotate(360deg);}.test2:hover img{opacity: 0.6;}.test2:hover figcaption p{transform: translate(0,0);opacity: 1;} 
        <!--旋转-->        <figure class="test2"><img src="img/altimg05.jpg"><figcaption>    <h2>图片标题</h2>    <p>飞来飞去</p>    <div></div></figcaption>        </figure>

demo03 说明:鼠标移入后 扭曲的字正常显示(因为例子中扭曲了90度 所以视觉上看不到文字)

      

步骤:

1.写好html代码并通过css设置好内容和图片的初始样式;

2.将文字内容扭曲90度 transform: skew(90deg);

3.接下来设置鼠标移入时(:hover)的样式 将文字内容扭曲0度 transform: skew(0);

/*扭曲*/.test3{background:#CCCCCC;}.test3 figcaption{position: absolute;left:15%;top:15%}.test3 figcaption h2{transform: skew(90deg);}.test3 figcaption p{transform: skew(90deg);}.test3:hover img{opacity: 0.6;}.test3:hover figcaption h2{transform: skew(0);}.test3:hover figcaption p{transform: skew(0);}
        <!--扭曲-->        <figure class="test3"><img src="img/altimg05.jpg"><figcaption>    <h2>图片标题</h2>    <p>这里是图片的相关描述内容</p></figcaption>        </figure>

demo04 说明:鼠标移入后 矩形和文字显示并缩小  图片也缩小

 

步骤:

1.写好html代码并通过css设置好内容和图片的初始样式

2.将内容放大1.2倍 这是为了鼠标移入后放大倍数变成1时形成缩小的效果 内容的透明度设置为0;

3.接下来设置鼠标移入时(:hover)的样式 内容放大倍数变成1也就是原始大小 图片缩小  透明度都变成1;

/*缩放*/.test4{background: #000;}.test4 figcaption{width: 100%;height: 100%;}.test4 figcaption h2{margin:15% 0 0 15%;opacity:0;transform: scale(1.2);}.test4 figcaption p{margin-left:15%;opacity:0;transform: scale(1.2);}.test4 figcaption div{border:2px solid #ccc;width: 80%;height: 80%;position:absolute;top:10%;left:10%;transform: scale(1.2,1.2);opacity: 0;}.test4:hover figcaption div{transform: scale(1,1);opacity: 1;}.test4:hover img{opacity: 0.6;transform: scale(0.9,0.9);}.test4:hover figcaption h2{opacity: 1;transform: scale(1);}.test4:hover figcaption p{opacity: 1;transform: scale(1);}

 

        <!--缩放-->        <figure class="test4"><img src="img/altimg05.jpg"><figcaption>    <h2>图片标题</h2>    <p>这里是图片的相关描述内容</p>    <div></div></figcaption>        </figure>

demo05 说明:鼠标移入后 内容显示 并出现井字格

  

步骤:

1.写好html代码并通过css设置好内容和图片的初始样式(井字就是两个矩形的重叠)

2.将两个矩形缩小0.8 并设置透明度为0 内容也设置透明度为0;

3.接下来设置鼠标移入时(:hover)的样式 内容透明度设置为1 设置矩形缩放为1  这里利用到transition属性 主要是为了缩小放大过程逐渐变化;

/*井字格*/.test5{background: #000;}.test5 figcaption{width: 100%;height: 100%;}.test5 figcaption h2{margin: 15% 0 0 18%;opacity: 0;}.test5 figcaption p{margin-left: 18%;opacity: 0;}.test5 figcaption div{position: absolute;}.test5 figcaption div.div01{width: 80%;height:70%;border-top: 2px solid #ccc;border-bottom: 2px solid #ccc;left:10%;top:15%;opacity: 0;transform: scale(0.8);}.test5 figcaption div.div02{width: 70%;height:80%;border-left: 2px solid #ccc;border-right: 2px solid #ccc;left: 15%;top:10%;opacity: 0;transform: scale(0.8);}.test5:hover div.div01{opacity: 1;transform: scale(1);transition: transform 0.3s ease-in}.test5:hover div.div02{opacity: 1;transform: scale(1);transition: transform 0.3s ease-in}.test5:hover figcaption p{opacity: 1;}.test5:hover figcaption h2{opacity: 1;}.test5:hover img{opacity: 0.6;}
        <!--井字格-->        <figure class="test5"><img src="img/altimg05.jpg"><figcaption>    <h2>图片标题</h2>    <p>这里是图片的相关描述内容</p>    <div class="div01"></div>    <div class="div02"></div></figcaption>        </figure>

以上是几个简单的小例子,之所以用figure和figcaption标签,主要是标签的语义化,截取动态图用到的是GifCam第一次用 挺好用的 很可爱 哈哈。

figure标签主要是用于规定独立的流内容(图片,图表,照片,代码等)而figcaption与figure标签配套使用,主要用于定义figure元素的标题

哦,对了,由于这几个例子写在一个html里面 所以提取出了部分公用的样式

body,figure,figcaption,h2,p{margin:0;padding:0;font-family: "微软雅黑";}figure{position: relative;overflow: hidden;float: left;width:33.33%;height: 350px;}figcaption{position: absolute;top: 0;left: 0;color:#fff;}figure img{width:101%;height: 360px;opacity: 0.8;transition: all 0.35s}figure figcaption p,h2,span,div{transition: all 0.35s}@media screen and (max-width: 600px) {    figure{width: 100%;}}@media screen and (min-width:601px) and (max-width: 1000px) {    figure{width: 50%;}}@media screen and (min-width: 1001px) {    figure{width: 33.33%;}} 

总结:

之所以选择写博客主要是为了锻炼自己的表达能力,培养一个总结知识点的好习惯,以前看别人写的一些好的文章时都很羡慕,自己却总是不知道从何下手,直到最近投简历的时候发现很多公司都要求注明自己的博客地址,所以有必要逼着自己写一下东西啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    css3实现画半圆弧线的示例代码
    下一条:
    浅谈css sticker-footer 布局
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客