Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud development gradients and animations


May 22, 2021 Mini Program Cloud Development Study Guide


Table of contents


CSS is the perfect combination of programming technology and design art, programming elegance lies in the clear readability of the code, and design elegance lies in the combination of technology to bring users a feast of vision and interaction. With CSS, not only can graphic designers often use filters, gradients and other design effects, but also can design some interactive animation, enhance the user experience.

The gradient of CSS

Color gradients are essential for designers, and the CSS linear-gradient() function is used to create a picture that represents linear gradients in two or more colors.

Technical documentation: CSS gradient property linear-gradient

Use the developer tool to create a new gradient page, and then enter the following code on the gradient.wxml page:

  1. <view class="gradient-display">
  2. </view>

Enter in gradient.wxss:

  1. .gradient-display{
  2. background-image:linear-gradient(red, blue);
  3. width: 100vw;
  4. height: 100vh;
  5. }
  6. .gradient-display{
  7. background-image:linear-gradient(red, blue);
  8. width: 100vw;
  9. height: 100vh;
  10. }

We found that because the background picture uses the linear-gradient property, its default gradient direction is from top to bottom, the first color (red here) is the start color, and the second color (here is blue blue) is the stop color.

Replace the values of backgound-image in .gradient-display with the following in turn:

  1. 改变渐变的方向
  2. background-image: linear-gradient(45deg, blue, red);
  3. /* 渐变轴为45度,从蓝色渐变到红色 */

You can also write like this, the specific meaning of the code can be referred to the technical documentation to understand

  1. background-image:linear-gradient(to left top, blue, red);
  2. /* 从右下到左上、从蓝色渐变到红色 */

Add more color changes

  1. background-image:linear-gradient(0deg, blue, green 40%, red);
  2. /* 从下到上(渐变轴为0度),从蓝色开始渐变、到高度40%位置是绿色渐变开始、最后以红色结束 */

The percentage of colors

  1. background-image: linear-gradient(19deg, rgb(33, 212, 253) 0%, rgb(183, 33, 255) 100%);

Small tasks: Refer to the radial gradient technical documentation below examples to implement a case of radial gradient in a small program. Through the actual way, understanding technical documents is like a dictionary, early learning does not have to do all understand, can be used on the line.

Filter filter

Filters are no strangers to designers, and CSS also has filter filter properties that allow you to perform Gaussus blur, adjust contrast, convert to grayscale images, color rotation, picture transparency, and more.

Compared to the filter effect of tools such as Photoshop, CSS can be used to process image filter effects in bulk, and programmatic means can not only overlay various effects, but also can be combined with interaction.

Here we mainly introduce the three most used filter effects, Gauss fuzzy bluur, picture grayscale (%), picture transparent opacity (%), other filter effects you can read the technical documentation later.

Technical documentation: Filter properties

Use the developer tool to create a new filter page and enter it at filter.wxml:

  1. <view class="filter-display">
  2. <view>blur高斯模糊</view>
  3. <image class="blur" mode="widthFix" src=//7n.w3cschool.cn/attachments/knowledge/202006/77455.png"></image>
  4. <view>grayscale图片变灰</view>
  5. <image class="grayscale" mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png"></image>
  6. <view>opacity图片透明</view>
  7. <image class="opacity" mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png"></image>
  8. <view>多个滤镜叠加,注意css的写法即可</view>
  9. <image class="multiple" mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png"></image>
  10. </view>

Then enter in filter.wxss:

  1. .filter-display img{
  2. width: 150px;height: auto;
  3. }
  4. .blur{
  5. filter: blur(8px);
  6. }
  7. .grayscale{
  8. filter: grayscale(90%);
  9. }
  10. .opacity{
  11. filter: opacity(25%);
  12. }
  13. .multiple{
  14. filter: blur(8px) grayscale(90%) opacity(25%);
  15. }

The picture changes from gray to color

In practice, many different color systems of pictures will be added to the site, such as partner logos, guest photos, news pictures, etc. , in order to make the photos and the site color system consistent, so all images need to be unified filter processing, and graying the picture is a more common practice.

Sometimes we add an interactive effect to these grayed-out pictures, which change from gray to color when the mouse hovers over the picture.

Enter the following code at filter.wxml:

  1. <view class="filter-display">
  2. <text>将鼠标悬停(模拟器)或手指(手机微信)按住或放开图片查看效果</text>
  3. <view class="grayscale" hover-class="grayscale-hover" >
  4. <image mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png"></image>
  5. </view>
  6. </view>

In the technical documentation view component, we can see that hover-class is the style class specified to press down.

Add the following styles to the filter.wxss:

  1. .filter-display image{
  2. width: 150px;height: auto;
  3. }
  4. .grayscale{
  5. filter: grayscale(90%);
  6. }
  7. .grayscale-hover{
  8. filter:grayscale(0);
  9. }

Gauss's fuzzy background

Gauss fuzzy is a special effect that UI designers often use. Graphic designers typically design images manually and manually, while UI designers can combine CSS to add uniform styles to all images in the same category, such as the background we want for each user's info page and the background at the top of each article.

Enter the following code at filter.wxml:

  1. <view class="userinfo-display">
  2. <view class="blur-bg"></view>
  3. <view class="user-img">
  4. <image src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png"></image>
  5. </view>
  6. </view>

Add the following styles to the filter.wxss:

  1. .blur-bg {
  2. width: 750rpx;height: 300rpx;overflow: hidden;
  3. background: url(https://hackwork-1251009918.cos.ap-shanghai.myqcloud.com/handbook/html5/blurimg.jpg);
  4. background-size: cover;
  5. position: fixed;
  6. filter: blur(15px);
  7. z-index: -1;
  8. }
  9. .user-img{
  10. width: 750rpx;height: 300rpx;
  11. display: flex;
  12. justify-content: center;
  13. align-items:center;
  14. }
  15. .user-img image {
  16. width: 80rpx;height: 80rpx;
  17. border-radius: 100%;
  18. }

UI designers don't work with web element designs like graphic designers can differentiate each element, after all, CSS can't be as complex as design tools like Photoshop, but he can do it in bulk. So compared to graphic designers, UI designers pay more attention to monotony and unity.

Deformed property Transform

The CSS transform property rotates, scales, tilts, or pans a given component by modifying the coordinate space of the CSS visual formatting model.

Regarding the technical documentation of transforming Transform, transition Transition, animation, we should not rush to study, a cursor look at it, and then have time to study it again.

Technical documentation: CSS deformation property transform

Use the WeChat developer tool to create a new transform page and enter the following code in transform.wxml

  1. <view class="transform-display">
  2. <view>缩放,scale(x,y),x为长度缩放倍数,y为宽度缩放倍数,如果只有一个值,则是长和宽缩放的倍数</view>
  3. <image class="scale" mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png" ></image>
  4. <view>平移,translate(x,y),x为x轴平移的距离,y为y轴平移的距离,如果只有一个值,则是x和y轴缩放的距离,值可以为负数</view>
  5. <image class="translate" mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png" ></image>
  6. <view>旋转,rotate()里的值为旋转的角度</view>
  7. <image class="rotate" mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png" ></image>
  8. <view>倾斜,skew()里的值为旋转的角度</view>
  9. <image class="skew" mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png" ></image>
  10. </view>

Add the following styles to the transform.wxss file:

  1. .transform-display image{
  2. width: 80px;height: 80px;
  3. }
  4. .scale{
  5. transform: scale(1,0.5);
  6. }
  7. .translate{
  8. transform: translate(500px,20px);
  9. }
  10. .rotate{
  11. transform: rotate(45deg);
  12. }
  13. .skew{
  14. transform: skew(120deg);
  15. }

Transition property Transition

CSS transitions control the transition effect of a component when it switches from one property state to another.

Technical documentation: CSS transition property Transition

It is recommended that we only use short-form properties transition, multiple properties together to write will be better, transition syntax is as follows, syntax is more complex, we can combine the actual case behind

  1. .selector {
  2. transition: [transition-property] [transition-duration] [transition-timing-function] [transition-delay];
  3. }

  • transition-property, the name of the CSS or animation property that applies the transition;
  • transition-duration, the entire transition effect lasts for a long time, the default time is 0 seconds, so to have a transition effect this must be defined;
    • transition-timing-function, which specifies the time curve of the transition effect, defaulting to ease;
    • transition-delay, how long the transition effect is delayed, or when to start, the default is 0 seconds, undefined words is to start directly;

Changes in background color

Also enter the following code into the page of the small program to see the effect in a real-world way.

Use the developer tool to create a new transition page, and then enter the following code in the transition.wxml page:

  1. <view class="transition-display">
  2. <view class="box bg-color" hover-class="bg-color-hover"></view>
  3. </view>

Then enter it in transition.wxss:

  1. .box{width: 150px;height: 150px;cursor: pointer;}
  2. .bg-color{
  3. background-color:green;
  4. }
  5. .bg-color-hover{
  6. background-color: yellow;
  7. transition: background-color 5s ease-out 3s;
  8. }

Animation needs to be triggered, here we are using hoverover-class to trigger the effect, put the mouse on the element for more than 8 seconds, to see what changes in the background color of the square.

Now that we understand the effect, let's combine the actual case to understand the syntax:

  • Because we use hover to trigger, so transition to be written in the element of the hover-class, the box in the background is green green, hover background is yellow yellow;
  • Because we change the box background-color, so transition needs to transition the CSS property name, that is, background-color;
  • The animation transition lasts for 5 seconds, which is when the background changes from green to yellow;
  • The ease-out here (slow end) is the time curve effect of the color transition. Y ou can also have linear (constant speed), ease-in (slow start), ease-in-out (slow start and slow end). In the case of a short duration, these time curve effect differences are subtle and require the designer to be sensitive enough to the animation.
  • The animation delay time is 3 seconds, which means that the animation does not start until 3 seconds later.

Technical documentation: A list of properties that can be animated

Let's look at a comprehensive case and enter it in transition.wxml

  1. <view>
  2. <text>盒子的多个属性一起动画: width, height, background-color, transform. 将鼠标或手指悬停在盒子上查看动画之后放开。</text>
  3. <view class="animatebox" hover-class="animatebox-hover"></view>
  4. </view>

Enter in transition.wxss

  1. .animatebox {
  2. display: block;
  3. width: 100px;
  4. height: 100px;
  5. background-color: #4a9d64;
  6. transition:width 2s, height 2s, background-color 2s, transform 2s;
  7. }
  8. .animatebox-hover {
  9. background-color: #cd584a;
  10. width:200px;
  11. height:200px;
  12. transform:rotate(180deg);
  13. }

Animated property Animation

CSS animations allow you to switch from one CSS style configuration to another. The animation consists of two parts: a style rule that describes the animation and keyframes that specify the start, end, and middle point styles of the animation.

Technical documentation: CSS animation property Animation

  1. <view class="fadeIn">
  2. <view>注意会有一个无限颜色渐变变化的动画</view>
  3. <image mode="widthFix" src="//7n.w3cschool.cn/attachments/knowledge/202006/77455.png" ></image>
  4. </view>

Enter the following code in wxss

  1. @keyframes fadeIn {
  2. from {
  3. opacity: 0;
  4. }
  5. to {
  6. opacity: 1;
  7. }
  8. }
  9. .fadeIn {
  10. animation: 4s linear 0s infinite alternate fadeIn;
  11. }

CSS3 animation library Animate .css

Animate .css is an interesting, cross-browser css3 animation library that allows you to add animation styles to specified elements by simply introducing a CSS file.

Technical documentation: Animate .css

It presets up to 80 animation effects, including shake, flash, bounce, flip, rotate (rotateIn/rotateOut), fade In/fadeOut, and more, including almost all common animation effects.

Small tasks: Refer to the .css of the animmate and implement a case of component jitter in a small program. In addition to introducing some style frameworks such as weui, there are some open source libraries that we can learn from, and more need to be learned later.