+9

UX Tip - Transition làm mọi thứ mượt mà hơn

Mayfest2023

1. Giới thiệu

1.1. Mở đầu chủ đề

Chào các bạn, hôm nay mình muốn chia sẻ với mọi người một kỹ thuật nhỏ nhưng đôi khi các bạn hay quên nó - kỹ thuật sử dụng transition trong CSS để tạo hiệu ứng mượt mà khi thay đổi trạng thái của một đối tượng.

Các bạn đã từng gặp trường hợp nào mà khi thay đổi trạng thái (ví dụ chiều dài hoặc chiều rộng) của một đối tượng, thì thay vì thay đổi một cách mượt mà, nó lại thay đổi một cách đột ngột không? Cảm giác như là mình đang bị giật mình vì sự thay đổi quá nhanh đó, đúng không nào?

1.2. Mục tiêu của bài viết

Trong bài viết này, mình sẽ chia sẻ với các bạn về cách sử dụng kỹ thuật transition trong CSS để làm cho sự thay đổi của trạng thái của một element trở nên mượt mà hơn, từ đó nâng cao trải nghiệm của người dùng (UX).

image.png

2. Hiểu về transition trong CSS

2.1. Giới thiệu về transition

Transition là một tính năng trong CSS giúp chúng ta kiểm soát tốc độ của một sự thay đổi. Ví dụ, nếu bạn muốn thay đổi chiều cao của một phần tử từ 0px sang 200px, thay vì thay đổi ngay lập tức, bạn có thể dùng transition để làm cho sự thay đổi diễn ra suốt một khoảng thời gian nhất định.

2.2. Cách sử dụng transition

Sử dụng transition trong CSS rất đơn giản. Cú pháp cơ bản như sau:

transition: property duration easing-function;

Trong đó:

  • property là thuộc tính bạn muốn áp dụng hiệu ứng transition, ví dụ: width, height, all (cho tất cả thuộc tính)
  • duration là thời gian hiệu ứng diễn ra, đơn vị thường là giây (s) hoặc mili giây (ms)
  • easing-function là hàm easing dùng để kiểm soát tốc độ của hiệu ứng. Có thể là linear, ease, ease-in, ease-out, ease-in-out hoặc cubic-bezier

Ví dụ về việc sử dụng transition trong CSS:

div {
    width: 100px;
    transition: width 2s ease-in-out;
}

div:hover {
    width: 200px;
}

2.3. Lợi ích của transition

Transition mang lại nhiều lợi ích trong việc nâng cao trải nghiệm của người dùng. Một số lợi ích chính gồm:

  • Tạo hiệu ứng chuyển đổi mượt mà, tạo cảm giác thoải mái và tự nhiên cho người dùng
  • Tăng độ sinh động và hấp dẫn của trang web
  • Kiểm soát được tốc độ và thời gian của sự thay đổi

image.png

3. Ứng dụng transition trong UX

3.1. Transition khi thay đổi trạng thái của một element

Khi thay đổi trạng thái của một element của một phần tử, việc sử dụng transition giúp tạo ra một hiệu ứng chuyển đổi mượt mà, không gây cảm giác giật mình cho người dùng.

Hãy cùng xem ví dụ sau:

.box {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    transition: all 0.5s ease-in-out;
}

.box:hover {
    width: 200px;
    height: 200px;
}

Trong ví dụ trên, khi bạn di chuyển chuột vào phần tử .box, chiều dài và chiều rộng của nó sẽ thay đổi từ 100px lên 200px. Nhưng thay vì thay đổi ngay lập tức, chúng ta sử dụng transition: all 0.5s ease-in-out; để làm cho sự thay đổi diễn ra suốt 0.5 giây, tạo ra một hiệu ứng chuyển đổi mượt mà.

3.2. Transition và smooth effect

Transition không chỉ giúp thay đổi chiều dài hoặc chiều rộng một cách mượt mà, nó còn giúp tạo ra nhiều hiệu ứng khác như thay đổi màu sắc, vị trí, độ nghiêng, độ trong suốt...

Mỗi khi bạn muốn thay đổi một thuộc tính nào đó của phần tử mà không muốn gây cảm giác giật mình cho người dùng, hãy nghĩ đến transition!

3.3. Ví dụ minh họa

Hãy cùng xem một ví dụ khác về việc sử dụng transition để tạo hiệu ứng khi di chuyển chuột vào một phần tử:

.button {
    padding: 10px 20px;
    background-color: blue;
    color: white;
    transition: background-color 0.3s ease-in-out;
}

.button:hover {
    background-color: red;
}

Trong ví dụ này, khi bạn di chuyển chuột vào phần tử .button, màu nền của nó sẽ thay đổi từ màu xanh sang màu đỏ. Nhưng nhờ có transition: background-color 0.3s ease-in-out;, sự thay đổi màu sắc sẽ diễn ra suốt 0.3 giây, tạo ra một hiệu ứng chuyển đổi màu sắc mượt mà.

4. Kết luận

Qua bài viết này, mình hi vọng các bạn đã hiểu rõ hơn về transition trong CSS và biết cách áp dụng nó để nâng cao UX, đặc biệt là khi thay đổi trạng thái của một element của các phần tử. Transition là một tính năng mạnh mẽ và linh hoạt, giúp chúng ta tạo ra những hiệu ứng chuyển đổi mượt mà và tăng độ sinh động cho trang web của mình.

Nhưng hãy nhớ rằng, tất cả mọi thứ đều nên có một mức độ vừa phải. Đừng quá lạm dụng hiệu ứng chuyển đổi, điều này có thể khiến trang web của bạn trở nên rối rắm và khó chịu. Mục tiêu cuối cùng là tạo ra một trang web dễ sử dụng và thoải mái cho người dùng.

Cuối cùng, đừng quên rằng transition: all 0.5s ease-in-out; là một mẹo hay mà đôi khi chúng ta hay quên, nhưng lại giúp mọi thứ trở nên mượt mà hơn.

Chúc các bạn thành công trong việc tạo ra những trang web thân thiện với người dùng và thú vị!

Hy vọng thông qua bài viết này, mình đã giúp các bạn hiểu rõ hơn về transition trong CSS và cách áp dụng nó để nâng cao trải nghiệm người dùng. Nếu có bất kỳ câu hỏi hoặc góp ý nào, đừng ngần ngại để lại phía dưới nhé. Mình sẽ rất vui lòng hỗ trợ bạn. Cảm ơn bạn đã đọc và hẹn gặp lại trong những bài viết tiếp theo!


ENGLISH VERSION

1. Introduction

1.1. Opening Statement

Hello everyone, today I want to share with you a small technique that you sometimes forget - the technique of using transition in CSS to create smooth effects when changing the state of an object.

Have you ever experienced a case where when you change the state (e.g., length or width) of an object, instead of smoothly transitioning, it changes abruptly? It feels like a sudden jolt, doesn't it?

1.2. Objective of the Article

In this article, I will share with you how to use the transition technique in CSS to make the state changes of an element smoother, thereby enhancing the user experience (UX).

2. Understanding CSS Transitions

2.1. Introduction to Transitions

Transition is a feature in CSS that allows us to control the speed of a change. For example, if you want to change the height of an element from 0px to 200px, instead of an immediate change, you can use transitions to make the change happen over a certain period of time.

2.2. How to Use Transitions

Using transitions in CSS is straightforward. The basic syntax is as follows:

transition: property duration easing-function;

Where:

  • property is the property you want to apply the transition effect to, e.g., width, height, all (for all properties).
  • duration is the time it takes for the transition effect to occur, usually in seconds (s) or milliseconds (ms).
  • easing-function is the easing function used to control the speed of the effect. It can be linear, ease, ease-in, ease-out, ease-in-out, or cubic-bezier.

Here's an example of using transitions in CSS:

div {
    width: 100px;
    transition: width 2s ease-in-out;
}

div:hover {
    width: 200px;
}

2.3. Benefits of Transitions

Transitions provide several benefits in enhancing the user experience. Some key benefits include:

  • Creating smooth transition effects, providing a comfortable and natural feel for users.
  • Increasing the liveliness and attractiveness of the website.
  • Having control over the speed and timing of the changes.

3. Applying Transitions in UX

3.1. Transitioning the State of an Element

When changing the state of an element, using transitions helps create a smooth transition effect without jarring the user.

Let's look at the following example:

.box {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    transition: all 0.5s ease-in-out;
}

.box:hover {
    width: 200px;
    height: 200px;
}

In the above example, when you hover over the .box element, its width and height will change from 100px to 200px. Instead of an immediate change, we use transition: all 0.5s ease-in-out; to make the transition last for 0.5 seconds, creating a smooth transition effect.

3.2. Transition and Smooth Effects

Transitions not only help smoothly change the width or height but also create various effects such as color changes, position changes, tilts, opacity changes, etc.

Whenever you want to change a property of an element without jarring the user, consider using transitions!

3.3. Illustrative Example

Let's take another example of using transitions to create an effect when hovering over a button:

.button {
    padding: 10px 20px;


    background-color: blue;
    color: white;
    transition: background-color 0.3s ease-in-out;
}

.button:hover {
    background-color: red;
}

In this example, when you hover over the .button element, its background color will change from blue to red. But thanks to transition: background-color 0.3s ease-in-out;, the color change will occur over 0.3 seconds, creating a smooth color transition effect.

4. Conclusion

Through this article, I hope you have a better understanding of transitions in CSS and how to apply them to enhance the user experience, especially when changing the state of elements. Transition is a powerful and flexible feature that allows us to create smooth transition effects and increase the vibrancy of our websites.

However, remember that everything should be used in moderation. Do not overuse transition effects, as it can make your website cluttered and unpleasant. The ultimate goal is to create a user-friendly and comfortable website.

Lastly, remember that transition: all 0.5s ease-in-out; is a handy tip that we sometimes forget, but it makes everything smoother.

Wishing you success in creating user-friendly and engaging websites!

I hope that through this article, I have helped you understand transitions in CSS and how to apply them to enhance the user experience. If you have any questions or suggestions, feel free to leave them below. I'll be happy to assist you. Thank you for reading, and see you in the next articles!


日本語バジョン

1. 紹介

1.1. はじめて

みなさん、こんにちは。今日はみなさんに忘れがちな小さなテクニックを共有したいと思います - CSSでオブジェクトの状態変化にスムーズな効果を作成するためにtransitionを使用する方法です。

オブジェクトの状態(例えば、幅や高さ)を変更するとき、それが滑らかに変わる代わりに、突然変化することがありませんか?急激な変化によって驚かされる感じがしますよね?

1.2. 記事の目的

この記事では、CSSでtransitionテクニックを使用して要素の状態変化をスムーズにし、ユーザーエクスペリエンス(UX)を向上させる方法について共有します。

2. CSSのtransitionについて理解する

2.1. transitionの概要

transitionはCSSの機能であり、変化の速度を制御するのに役立ちます。例えば、要素の高さを0pxから200pxに変更したい場合、即座に変更する代わりに、transitionを使用して一定の時間内で変化が行われるようにすることができます。

2.2. transitionの使い方

CSSでtransitionを使用することは非常に簡単です。基本的な構文は次のとおりです。

transition: property duration easing-function;

ここで:

  • propertyは適用したい遷移効果のプロパティです。例えば、width、height、all(すべてのプロパティに対して適用)
  • durationは効果の時間を示し、単位は秒(s)またはミリ秒(ms)です。
  • easing-functionは遷移効果の速度を制御するためのイージング関数です。linear、ease、ease-in、ease-out、ease-in-out、またはcubic-bezierを使用できます。

CSSでtransitionを使用する例を見てみましょう。

div {
    width: 100px;
    transition: width 2s ease-in-out;
}

div:hover {
    width: 200px;
}

2.3. transitionの利点

transitionはユーザーエクスペリエンスの向上に多くの利点をもたらします。いくつかの主な利点は次のとおりです:

  • スムーズなトランジション効果を作成し、ユーザーに快適で自然な感覚を与えることができます。
  • ウェブページの活気と魅力を高めます。
  • 変化の速度と時間を制御できます。

3. UXにおけるtransitionの応用

3.1. 要素の状態変化時のtransition

要素の状態を変化させる際に、transitionを使用すると、ユーザーに突然の変化を感じさせずに、スムーズなトランジション効果を作成することができます。

以下の例を見てみましょう。

.box {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    transition: all 0.5s ease-in-out;
}

.box:hover {
    width: 200px;
    height: 200px;
}

上記の例では、.box要素にマウスを移動すると、幅と高さが100pxから200pxに変化します。しかし、即座に変更される代わりに、transition: all 0.5s ease-in-out;を使用して、変化が0.5秒間続くようにし、スムーズなトランジション効果を作成します。

3.2. Transitionとスムーズなエフェクト

transitionは、幅や高さだけでなく、色、位置、傾斜、透明度など、さまざまなエフェクトを作成するのに役立ちます。

ユーザーに違和感を与えずに要素の属性を変更したい場合は、transitionを考えてみてください。

3.3. イラストレーションの例

要素にマウスを移動すると、効果が発生するようにtransitionを使用する例を見てみましょう。

.button {
    padding: 10px 20px;
    background-color: blue;
    color: white;
    transition: background-color 0.3s ease-in-out;
}

.button:hover {
    background-color: red;
}

上記の例では、.button要素にマウスを移動すると、背景色が青から赤に変化します。しかし、transition: background-color 0.3s ease-in-out;のおかげで、色の変化が0.3秒間続き、滑らかな背景色のトランジション効果が生じます。

4. 結論

この記事を通じて、CSSのtransitionについて理解し、要素の状態変化時にそれを適用してUXを向上させる方法を学んでもらえたらと思います。transitionは、滑らかなトランジション効果を作成し、ウェブサイトの活気を高めるための強力で柔軟な機能です。

ただし、すべてのもの

は適度なバランスが必要です。過度にトランジション効果を使用すると、ウェブサイトが混乱し、ユーザーにとって使いづらいものになる可能性があるので注意してください。最終目標は、ユーザーフレンドリーで快適なウェブサイトを作成することです。

最後に、transition: all 0.5s ease-in-out;は忘れがちな便利なヒントですが、すべてをスムーズにします。

ユーザーフレンドリーで楽しいウェブサイト作りの成功を祈っています!

この記事を通じて、CSSのtransitionについて理解し、ユーザーエクスペリエンスを向上させるための応用方法について共有できたら幸いです。質問や提案がある場合は、お気軽にコメントしてください。お手伝いできることを嬉しく思います。読んでいただきありがとうございました。次回の記事でお会いしましょう!

Mình hy vọng bạn thích bài viết này và học thêm được điều gì đó mới.

Donate mình một ly cafe hoặc 1 cây bút bi để mình có thêm động lực cho ra nhiều bài viết hay và chất lượng hơn trong tương lai nhé. À mà nếu bạn có bất kỳ câu hỏi nào thì đừng ngại comment hoặc liên hệ mình qua: Zalo - 0374226770 hoặc Facebook. Mình xin cảm ơn.

Momo: NGUYỄN ANH TUẤN - 0374226770

TPBank: NGUYỄN ANH TUẤN - 0374226770 (hoặc 01681423001)

image.png


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí