Goal: Use displacement, scaling, rotation, and gradient effects to enrich the presentation of web page elements.
Article directory
01-Plane conversion
Introduction
Function: Add dynamic effects to elements, generally used in conjunction with transitions
Concept: Change the shape of the box in the plane (displacement, rotation, scaling, tilt)
Plane transformation is also called 2D transformation, and its attribute is transform
Pan
transform: translate(X轴移动距离, Y轴移动距离);
- value
- Pixel unit value
- Percentage ( calculated based on the size of the box itself )
- Either positive or negative
- Skill
- translate() only writes one value , which means moving along the X axis
- Set the X or Y axis movement distance individually: translateX() or translateY()
Center positioning
- Method 1: margin
- Method 2: Translation → The percentage is calculated based on the size of the box itself
Case-double door
- HTML structure
<div class="father">
<div class="left"></div>
<div class="right"></div>
</div>
- CSS styles
* {
margin: 0;
padding: 0;
}
/* 1. 布局:父子结构,父级是大图,子级是左右小图 */
.father {
display: flex;
margin: 0 auto;
width: 1366px;
height: 600px;
background-image: url(./images/bg.jpg);
overflow: hidden;
}
.father .left,
.father .right {
width: 50%;
height: 600px;
background-image: url(./images/fm.jpg);
transition: all .5s;
}
.father .right {
/* right 表示的取到精灵图右面的图片 */
background-position: right 0;
}
/* 2. 鼠标悬停的效果:左右移动 */
.father:hover .left {
transform: translate(-100%);
}
.father:hover .right {
transform: translateX(100%);
}
rotate
transform: rotate(旋转角度);
- Value: The unit of angle is deg
- Skill
- The value can be positive or negative
- If the value is positive, rotate clockwise
- If the value is negative, rotate counterclockwise
Transform origin
By default, the transformation origin is the box center point
transform-origin: 水平原点位置 垂直原点位置;
Value:
- Positional nouns (left, top, right, bottom, center)
- Pixel unit value
- percentage
Case-Clock
.hour {
width: 6px;
height: 50px;
background-color: #333;
margin-left: -3px;
transform: rotate(15deg);
transform-origin: center bottom;
}
.minute {
width: 5px;
height: 65px;
background-color: #333;
margin-left: -3px;
transform: rotate(90deg);
transform-origin: center bottom;
}
.second {
width: 4px;
height: 80px;
background-color: red;
margin-left: -2px;
transform: rotate(240deg);
transform-origin: center bottom;
}
multiple transformations
Multiple transformation techniques: first translate and then rotate
transform: translate() rotate();
- Principle of multiple transformations: Transform the form based on the coordinate axis of the first transformation method.
- Rotation will change the coordinate axis of web page elements
- If you write the rotation first, the axis direction of the subsequent transformation effect will be based on the axis direction after rotation, which will affect the transformation result.
Zoom
transform: scale(缩放倍数);
transform: scale(X轴缩放倍数, Y轴缩放倍数);
- Skill
- Usually, just set a value for scale(), indicating that the X-axis and Y-axis are scaled equally.
- A value greater than 1 means zooming in, a value less than 1 means zooming out.
Case-Playback special effects
- CSS styles
/* 1. 摆放播放按钮:图片区域的中间 */
.box li {
overflow: hidden;
}
.pic {
position: relative;
}
.pic::after {
position: absolute;
left: 50%;
top: 50%;
/* margin-left: -29px;
margin-top: -29px; */
/* transform: translate(-50%, -50%); */
content: '';
width: 58px;
height: 58px;
background-image: url(./images/play.png);
transform: translate(-50%, -50%) scale(5);
opacity: 0;
transition: all .5s;
}
/* 2. hover效果:大按钮,看不见:透明是0 → 小按钮,看得见:透明度1 */
.box li:hover .pic::after {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
tilt
transform: skew();
Value: angle degree deg
02-Gradient
Gradient is the effect of gradual changes in multiple colors. It is generally used to set the background of a box.
Classification:
-
linear gradient
-
radial gradient
linear gradient
background-image: linear-gradient(
渐变方向,
颜色1 终点位置,
颜色2 终点位置,
......
);
Value:
- Gradient direction: optional
- to directional noun
- Angle degree
- End position: optional
- percentage
Case-Product Display
- HTML structure
<div class="box">
<img src="./images/product.jpeg" alt="" />
<div class="title">OceanStor Pacific 海量存储斩获2021 Interop金奖</div>
<div class="mask"></div>
</div>
- CSS styles
.mask {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-image: linear-gradient(
transparent,
rgba(0,0,0,0.5)
);
opacity: 0;
transition: all .5s;
}
.box:hover .mask {
opacity: 1;
}
radial gradient
background-image: radial-gradient(
半径 at 圆心位置,
颜色1 终点位置,
颜色2 终点位置,
......
);
Value:
- The radius can be 2, then it is an ellipse
- Value of circle center position: pixel unit value/percentage/orientation noun
03-Comprehensive case
Navigation-Channel
Arrow rotation
.x-header-nav .nav-item:hover .icon-down {
transform: rotate(-180deg);
}
Channel list
.channel-layer {
position: absolute;
top: 60px;
left: 50%;
z-index: -2;
width: 1080px;
height: 120px;
padding: 10px;
margin-left: -540px;
color: #72727b;
background-color: #f5f5f5;
border: 1px solid #e4e4e4;
border-top: none;
transition: all 0.5s;
transform: translateY(-120px);
}
/* TODO 2. 弹窗频道 */
.x-header-nav .nav-item:hover .channel-layer {
transform: translateY(0);
}
gradient button
search button
.x-header-search form .btn {
position: absolute;
top: 0;
right: 0;
width: 60px;
height: 40px;
line-height: 40px;
text-align: center;
background-color: #f86442;
border-top-right-radius: 20px;
border-bottom-right-radius: 20px;
background-image: linear-gradient(
to right,
rgba(255, 255, 255, 0.3),
#f86442
);
}
Login button
/* TODO 7. 渐变按钮 */
.card .card-info .login {
padding: 3px 34px;
color: #fff;
background-color: #ff7251;
border-radius: 30px;
box-shadow: 0 4px 8px 0 rgb(252 88 50 / 50%);
background-image: linear-gradient(
to right,
rgba(255, 255, 255, 0.2),
#ff7251
);
}
client button
/* TODO 8. 径向渐变 */
.download .dl .dl-btn {
width: 68px;
height: 34px;
line-height: 34px;
color: #fff;
text-align: center;
border-radius: 4px;
background-image: radial-gradient(
50px at 10px 10px,
rgba(255, 255, 255, 0.5),
transparent
);
}
carousel
/* TODO 4. 摆放图片 */
.banner .banner-list .banner-item.left {
z-index: 0;
transform: translate(-160px) scale(0.8);
transform-origin: left center;
}
.banner .banner-list .banner-item.right {
z-index: 0;
transform: translate(160px) scale(0.8);
transform-origin: right center;
}
you may also like
/* TODO 5. 播放按钮和遮罩 */
.album-item .album-item-box::after {
position: absolute;
left: 0;
top: 0;
content: '';
width: 100%;
height: 100%;
background: rgba(0,0,0,.5) url(../assets/play.png) no-repeat center / 20px;
opacity: 0;
transition: all .5s;
}
.album-item .album-item-box:hover::after {
opacity: 1;
background-size: 50px;
}
/* TODO 6. 图片缩放 */
.album-item .album-item-box:hover img {
transform: scale(1.1);
}