- HTML5 Web Application Development By Example Beginner's Guide
- J.M. Gustafson
- 593字
- 2021-08-13 16:50:24
Time for action – effects in action
Let's add some effects to the task list. First, we will add the ability to select a task in the list. When a task is clicked, it will grow in size and get a colored border so it's easy to tell that it is selected. We will also add a hover effect to the tasks so that when the user moves the mouse over a task, the task's action buttons are shown. When the mouse moves off of a task, the buttons will fade back out. You can find the code for this section in chapter2/example2.2
.
The first thing we need to do is go back to taskAtHand.js
and add a click
event handler to the task
element after it is created in the addTaskElement()
method:
$task.click(function() { onSelectTask($task); });
It calls the onSelectTask()
method when a task is clicked. In this method we will mark a task
element as selected by giving it a class name of selected
. We will also remove the selected
class from the previously selected task element:
function onSelectTask($task) { if ($task) { // Unselect other tasks $task.siblings(".selected").removeClass("selected"); // Select this task $task.addClass("selected"); } }
Now let's add a style in taskAtHand.css
for the selected task. We will increase the padding to make the element bigger, add a border to highlight it, and change the background color:
#task-list .task.selected { padding: 0.6em 0.5em; border: 2px solid orange; border-radius: 6px; background-color: white; }
That's nice, but we can make it better by adding a transition. We will add the transition
property to the .task
class. It will ease in all property changes over one quarter of a second. This will provide some nice visual feedback to the user when they select a task:
#task-list .task { /* Not shown... */ -webkit-transition: all 0.25s ease; -moz-transition: all 0.25s ease; -o-transition: all 0.25s ease; transition: all 0.25s ease; }
While we're at it, let's add one more transition. We will hide the task action buttons until the user moves the mouse over a task or selects a task. To do that, all we need to do is add a little more CSS. First, we will hide the task buttons' container element by setting its opacity
property to 0
to make it transparent. Then we add the same transition
properties as we did previously:
#task-list .task .tools { position: absolute; top: 0.25em; right: 0.25em; border: 1px solid black; border-radius: 2px; opacity: 0; -webkit-transition: all 0.25s ease; -moz-transition: all 0.25s ease; -o-transition: all 0.25s ease; transition: all 0.25s ease; }
Now we add a hover
selector for the task
element that sets the opacity
property to 1
to make it opaque. This, along with the transition, will make the task buttons appear to fade in when the user hovers over a task. We also add a selector to make the task buttons show up when a task is selected (the second line in the following snippet):
#task-list .task:hover .tools, #task-list .task.selected .tools { opacity: 1; }
Before CSS3, you could do the same thing with JavaScript using the jQuery fadeIn()
and fadeOut()
methods along with some mouse events, but it required considerably more code.
What just happened?
We added some CSS3 transitions to the task list to make the task item buttons fade in and out and make selected task items grow larger when clicked. We've seen that with just a few lines of CSS we can add some nice effects to our applications. Here's what our task list looks like now with Task 2 selected:

- The Complete Rust Programming Reference Guide
- Mastering AWS Lambda
- 無代碼編程:用云表搭建企業(yè)數(shù)字化管理平臺(tái)
- Mastering Natural Language Processing with Python
- JMeter 性能測(cè)試實(shí)戰(zhàn)(第2版)
- Python零基礎(chǔ)快樂學(xué)習(xí)之旅(K12實(shí)戰(zhàn)訓(xùn)練)
- Django:Web Development with Python
- SQL基礎(chǔ)教程(視頻教學(xué)版)
- 可解釋機(jī)器學(xué)習(xí):模型、方法與實(shí)踐
- The Professional ScrumMaster’s Handbook
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)實(shí)驗(yàn)指導(dǎo)與習(xí)題選解(第2版)
- 30天學(xué)通C#項(xiàng)目案例開發(fā)
- C語言從入門到精通
- Developing Java Applications with Spring and Spring Boot
- Java EE程序設(shè)計(jì)與開發(fā)實(shí)踐教程