單選按鈕設置為被選中狀態_為什么要設置錯誤的按鈕狀態

單選按鈕設置為被選中狀態

當正確的方法出錯時 (When the right way goes wrong)

Let’s say you want to create a click effect on an HTML button. The first idea that many people get is to do something that reproduces the feeling of the sound emitted by a real button. Here is a simple pulse effect:

假設您要在HTML按鈕上創建點擊效果。 許多人得到的第一個想法是做一些能夠重現真實按鈕發出聲音的感覺的事情。 這是一個簡單的脈沖效果:

Image for post
Button with a pulse effect on click
帶有脈沖效果的按鈕

To create a button effect, in HTML/CSS, you have only three states to choose from:

要創建按鈕效果,在HTML / CSS中, 只有三種狀態可供選擇

  • :hover which is triggered by having your cursor over the button.

    :hover ,通過將光標懸停在按鈕:hover觸發。

  • :focus which is triggered when you give focus to the button using your keyboard or by clicking it. For some browsers, the focus is given when you press your mouse button down. For others, it’s when you release it.

    :focus ,當您使用鍵盤或單擊按鈕將焦點放在按鈕上時觸發。 對于某些瀏覽器,當您按下鼠標按鈕時會給出焦點。 對于其他人,就是您發布它的時間。

  • :active which is triggered from the moment you press your mouse button down to when you release it.

    :active ,從按下鼠標按鈕到釋放鼠標之間觸發。

According to this, it’s safe to assume that the good state to style is :active since it comes and goes as you press the button, right? Depending on the effect you want to create, that can be absolutely wrong.

據此,可以安全地認為樣式的良好狀態是:active因為在您按下按鈕時它就會來回移動,對嗎? 根據要創建的效果,這可能是絕對 錯誤的

This is what will happen in our case if we use :active:

如果使用:active這就是我們的情況:

Image for post

Looking closely, you can see our pulse effect triggering on click, then immediately reverting to nothing. What happened?

仔細觀察,您會發現我們的脈沖效果在點擊時觸發,然后立即恢復為零。 發生了什么?

了解問題 (Understanding the problem)

This pulse effect takes 300ms to go through its whole animation. A mouse click lasts anywhere between ≈15ms (gaming mouse) and 200+ ms (laptop trackpad).

此脈沖效果需要300毫秒才能經歷整個動畫。 鼠標單擊的持續時間介于≈15ms (游戲鼠標) 和200+ ms (筆記本電腦觸控板)之間。

Image for post

You guessed it, your CSS transition, although set to 300ms, will stop halfway because the :active state will disappear. You can’t control the actual transition duration this way. Many developers don’t notice that when implementing short or static effects, but in our case, it just can’t work.

您猜對了,您CSS過渡盡管設置為300ms,但會中途停止,因為:active狀態將消失。 您無法通過這種方式控制實際的過渡時間。 許多開發人員沒有注意到在實現短效果或靜態效果時,但是在我們的情況下,它根本無法工作。

解決方案 (The solution)

There are two tricks to achieve this effect. The first one is to start the click effect when the mouse button is released. To do so, the state you are looking for is :not(:active).

有兩種技巧可以達到這種效果。 第一個是在釋放鼠標按鈕時開始單擊效果 。 為此,您正在尋找的狀態是:not(:active)

By using this state, the animation will start when the :active state disappears, and there will be no limit for the transition duration!

通過使用此狀態,動畫將在:active狀態消失時開始,并且過渡持續時間沒有限制!

Image for post

But there is one more thing: it should only animate one way. You must put the transition property for this effect under the :not(:active) selector. If you don’t, the transition will try to go back and forth and result in a weird effect or simply no effect at all.

但是還有一件事: 它只能以一種方式進行動畫處理 。 您必須將此效果的transition屬性放在:not(:active)選擇器下。 如果您不這樣做,則過渡將嘗試來回移動,從而導致怪異的效果,或者根本沒有效果。

For this effect, I actually styled the :before pseudo-element. Here is the relevant part of the code:

為此,我實際上設置了:before偽元素的樣式。 這是代碼的相關部分:

The previous sample illustrates our case very well but needs some more code to work. You can get the full code sample in this gist.

前面的示例很好地說明了我們的情況,但是還需要更多代碼才能工作。 您可以 在本要點中 獲得完整的代碼示例

Now you can unleash your best transitions and create crazy click effects that won’t stop randomly! Here is another example that goes for a whole second:

現在,您可以釋放最佳的過渡效果,并創建不會隨機停止的瘋狂點擊效果! 這是一秒鐘的例子:

Image for post
Another click effect example using this method.
使用此方法的另一個點擊效果示例。

Just remember that this method is using transitions, and not keyframes, which are managing their own timelines.

請記住,此方法使用的是過渡,而不是關鍵幀,它們是在管理自己的時間軸。

This method works really well with pseudo-elements and you should get some fun hours creating weird stuff with it (I did). If you need more than two elements, just put divs inside the button tag.

這種方法對偽元素非常有效,您應該花一些時間來創建一些奇怪的東西(我這樣做了)。 如果需要兩個以上的元素,只需將div放在button標記內。

Image for post
Example using a div inside the button
在按鈕內使用div的示例

結論 (Conclusion)

What you need to remember to create beautiful click effects using this method:

使用此方法創建漂亮的點擊效果需要記住的內容:

  • You must target the :not(:active) selector so that the animation starts at the end of a click.

    您必須定位:not(:active)選擇器,以便動畫在單擊結束時開始

  • The transition property for your effect also need to be set on the :not(:active) selector, so that the transition goes only one way.

    還需要在:not(:active)選擇器上設置效果的過渡屬性,以便過渡只能以一種方式進行

  • It only uses transitions because keyframes manage their own timelines.

    它僅使用過渡,因為關鍵幀管理自己的時間軸。

  • Using pseudo-elements or internal div tags allow you to create effects outside of the button.

    使用偽元素或內部div標簽可讓您在按鈕外部創建效果。

Have fun animating!

玩得開心!

翻譯自: https://codeburst.io/why-you-should-style-the-wrong-button-state-d4e4c2db8f7e

單選按鈕設置為被選中狀態

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275479.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275479.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275479.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

「娃娃分享」-常見自校檢分析實例.

自校檢是許多軟件的保護手段之一,對軟件加個簡單的殼再增加自校檢在一定程序上可以抵擋住一大部分新手,不過,對許多人來說,這個保護已經很弱了。。下面講幾種常見的解決自校檢方法,寫的粗略,希望大家補充。…

用VC和MinGW導出dll的def和lib(a)文件

為什么80%的碼農都做不了架構師?>>> 原文地址:http://zhangyafeikimi.iteye.com/blog/404580 有了dll文件需要導出def文件: pexports zlib1.dll > zlib1.def 有了dll和def文件,需要導出MinGW的.a文件:…

51中斷編程c語言,[新人求指教]51C語言編程可否用中斷令循環結束提早結束

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓C51_C語言編程控制流水燈硬件電路 p0 接 led 8 個,P33 接按鍵使用中斷2開機燈按1~8逐位閃爍,并循環按鍵后改為 兩燈亮 的流水燈下面寫了個程序#include #include #define uchar unsigned char#define uint u…

產品設計美學案例分析_美學在產品設計中的重要性

產品設計美學案例分析重點 (Top highlight)In one of my previous jobs, I had really interesting debates with the CEO regarding whether we should spend more time improving the way our app looks and feels. ‘How could he not care that the design is outdated?! …

即將到來的ECMAScript 2022標準

大家好,我是若川。周末分享一篇相對簡單的文章。最近組織了源碼共度活動:1個月,200人,一起讀了4周源碼,參與的小伙伴都表示收獲很大。如果感興趣可以點擊鏈接掃碼加我微信 ruochuan12。另外:昨天的推文入門…

c語言中二叉樹中總結點,C語言二叉樹的三種遍歷方式的實現及原理

二叉樹遍歷分為三種:前序、中序、后序,其中序遍歷最為重要。為啥叫這個名字?是根據根節點的順序命名的。比如上圖正常的一個滿節點,A:根節點、B:左節點、C:右節點,前序順序是ABC(根節…

動態庫的創建與使用

1、動態庫文件的創建 (1)編寫源文件 (2)編譯生成動態庫 g -fPIC -shared -o libfile_operation.so file_operation.cpp 此編譯過程分為兩步,等同于下面的兩個命令: g -c -fPIC file_operation.cpp …

ux設計中的各種地圖_UX寫作中的移情

ux設計中的各種地圖Demetri Martin is a master of comedic situations. If you’ve never seen Demetri Martin是喜劇情境的大師。 如果你從未見過 him before, he has a sort of dry brand of observational humor, relying more on anecdotes than full stories, and often…

字符串搜索。HOJ1530 Compound Words。

stl set實現字符串搜索。。效率一般。(附二分搜索。) Compound WordsTime limit:1sec.Submitted:233Memory limit:32MAccepted:81Source: Waterloo ACM Programming Contest Sep 28, 1996 You are to find all the two-word compound words in a dictionary. A two-word compo…

字節3-1前端面試官自學Vue的正確姿勢

大家好,我是若川。前不久和一個字節前端TL朋友聊天,說到大廠前端供需脫節的情況。特別是使用Vue框架的,因為簡單易學好上手,但是能夠深入理解的人并不多,大多都只停留在應用層面,缺乏更深層面的理解。尤其是…

android視圖工具,android studio的HierarchyViewer工具如何知道android屏幕的視圖屬性

讓我們首先看看adb是如何組織的.它有3個主要組件,如here所述 –> client – 在用于開發的機器上運行的客戶端.通過發出adb命令從shell調用客戶端.層次結構查看器還會創建adb客戶端.> server – 在開發計算機上作為后臺進程運行的服務器.它將從adb客戶端發出的命令傳遞給a…

云時代架構讀后感4--IT架構的本質

IT架構的本質 原文地址:http://mp.weixin.qq.com/s?__bizMzAwNTQ4MTQ4NQ&mid2453562304&idx1&snbe86a7bc682c4e76e06b87a10ad45188&chksm8cd136a2bba6bfb430103e50f94b670e799412d0a1cae4eded0eb901847b6d462359ae317635&mpshare1&scene23…

蘋果風格ui_蘋果如何使Soft-UI成為未來

蘋果風格ui重點 (Top highlight)Apple announced some pretty wild updates at WWDC 2020 today.蘋果今天在WWDC 2020上宣布了一些相當瘋狂的更新。 But technology aside, let’s focus on how their UI has changed. It went through the first bitmap representations, thr…

【數據結構】量子危機

問題 宇宙時間公元 5.55 億年,由于某種原因兩大聯盟展開了激戰(maxingc 聯盟采用了微子技術): 邪惡的 maxingc 聯盟采集好了微子能,就要運輸。Maxingc 聯盟的領袖 xc 此時才發現,自己的軍事基地中由微子發射…

android 自定義menu背景,Android編程實現自定義系統菜單背景的方法

本文實例講述了Android編程實現自定義系統菜單背景的方法。分享給大家供大家參考,具體如下:不多說,上圖,見代碼。package lab.sodino.menutest;import android.content.Context;import android.app.Activity;import android.os.Bu…

面試官問 async、await 函數原理是在問什么?

大家好,我是若川。這是 源碼共讀活動《1個月,200人,一起讀了4周源碼》 第四期,紀年小姐姐的第四次投稿。紀年小姐姐通過本次學習提早接觸到generator,協程概念,了解了async/await函數的原理等。第四期是 學…

一步步優化JVM六:優化吞吐量[轉]

2019獨角獸企業重金招聘Python工程師標準>>> 原文:http://ganlv.iteye.com/blog/1571315 參考:http://www.myexception.cn/software-architecture-design/1455594.html 現代JVM是一個具有靈活適應各種應用能力的軟件,盡管很多應用…

element-ui 網格_UI備忘單:列表與網格

element-ui 網格重點 (Top highlight)Grids or lists? That is the question we will look at in this cheat sheet. While they can be used anywhere in your site, we are going to look primarily at search results, catalogs and newsfeeds. Making this choice will de…

asp.net mvc使用TagBuilder的應用程序集

在asp.net mvc編寫擴展方法中需要使用到TagBuilder類,根據msdn的說法應該應用System.Web.Mvc.dll 程序集。 TagBuilder 構造函數 .NET Framework 4 初始化 TagBuilder 類的新實例。命名空間: System.Web.Mvc程序集: System.Web.Mvc&#xf…

50行 koa-compose,面試常考的中間件原理原來這么簡單?

大家好,我是若川。源碼共讀《1個月,200人,一起讀了4周源碼》 活動第五期是學習 koa 源碼的整體架構,淺析koa洋蔥模型原理和co原理中的koa-compose源碼原理,閱讀不到50行的koa-compose源碼。這篇是izjing小哥哥的投稿。…