您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在CSS3中,过渡效果可以通过transition
属性来实现
style
属性或使用外部CSS样式表来完成。/* 使用内部样式表 */
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
/* 添加过渡效果 */
transition: all 2s ease;
}
.box:hover {
width: 200px;
height: 200px;
background-color: red;
}
</style>
<!-- 使用外部样式表 -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="box"></div>
</body>
</html>
在上面的示例中,.box
元素在鼠标悬停时,宽度和高度会从100px变为200px,背景颜色从蓝色变为红色。过渡效果将在2秒内完成,使用ease
缓动函数。
transition
属性应用于单个CSS属性,而不是所有属性。这样,只有指定的属性会在过渡期间发生变化。.box {
width: 100px;
height: 100px;
background-color: blue;
/* 只对宽度和高度应用过渡效果 */
transition: width 2s ease, height 2s ease;
}
transition-delay
属性设置延迟时间。.box {
width: 100px;
height: 100px;
background-color: blue;
transition: all 2s ease;
/* 添加2秒的延迟 */
transition-delay: 2s;
}
transition
属性应用于包含这些元素的父容器。然后,使用:hover
伪类为子元素定义目标状态。<style>
.container {
width: 200px;
height: 200px;
background-color: lightgray;
}
.container:hover .box {
transform: translateY(100px);
}
.box {
width: 100px;
height: 100px;
background-color: blue;
/* 添加过渡效果 */
transition: transform 1s ease;
}
</style>
<div class="container">
<div class="box"></div>
</div>
在这个例子中,当鼠标悬停在.container
元素上时,其子元素.box
会向下移动100px。过渡效果将在1秒内完成,使用ease
缓动函数。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。