In this project, you will learn how to build a simple e-book reader using Vue 2.x. The e-book reader will allow users to toggle the visibility of the header toolbar, set the reading theme, and adjust the font size of the text content.
👀 Preview
🎯 Tasks
In this project, you will learn:
How to implement the toggle of the header toolbar's visibility
How to implement the setting of the reading theme
How to implement the setting of the font size
🏆 Achievements
After completing this project, you will be able to:
Use Vue.js to create interactive user interfaces
Handle user interactions and update the UI accordingly
Dynamically style elements based on user preferences
Ensure the application remains responsive and accessible
Implement the Toggle of the Header Toolbar’s Visibility
To get started, look at the directory structure of the files on the left as follows:
├── index.html
├── css
└── js
└── vue.js
Where:
index.html is the main page and the next file you need to modify.
css is the folder for style files.
js/vue.js is the Vue 2.x file.
Next you do everything in the index.html file.
In this step, you will learn how to implement the toggle of the header toolbar's visibility. Follow the steps below to complete this step:
Open the index.html file, define the variable isShowTools in data under el: "#app" to control the display of the header toolbar, with a default value of true to show the header toolbar.
data: {
isShowTools: true,
// ...
}
Wrap the header toolbar content (the <div class="header"> element) in a <transition> element with the v-if directive, binding it to the isShowTools data property. This will ensure that the header toolbar is only visible when isShowTools is true.
In the index.html file, locate the <a> element with the class iconfont icon-setting. This element is used to toggle the visibility of the header toolbar.
Add a @click directive to the <a> element, and bind it to a showTools method in the Vue instance. This method will toggle the isShowTools data property, which controls the visibility of the header toolbar.
In this step, you will learn how to implement the setting of the reading theme. Follow the steps below to complete this step:
Define the variable isActiveColor in the data under el: "#app" and set the default value to 0, i.e. the default background color is the first selected color. In the index.html file, locate the <div class="right" id="setBG"> element. This is where the circular color blocks for the reading theme will be displayed.
data: {
isActiveColor: 0,
// ...
}
Use a v-for directive to loop through the bgList data property, which contains the list of theme colors. For each color, create an <a> element with a @click directive that calls the changeColor method, passing the current index as an argument.
In the Vue instance, add the changeColor method to the methods object. This method should update the isActiveColor data property with the index of the selected color block.
In the <p> element with the class text-content, use the :style directive to dynamically set the background color and font color based on the selected theme color.
In this step, you will learn how to implement the setting of the font size. Follow the steps below to complete this step:
Define the variable "fontSize" in data under el: "#app" and set the default value to "18". In the index.html file, locate the <div class="set-font"> element. This is where the font size controls will be displayed.
data: {
fontSize: 18,
// ...
}
Locate <span class="lang">18</span> and change 18 to the fontSize variable.
<span class="lang">{{ fontSize }}</span>
Add @click directives to the "A-" and "A+" buttons, binding them to the updateFontSize method in the Vue instance. Pass a boolean value to indicate whether the font size should be increased or decreased.
In the Vue instance, add the updateFontSize method to the methods object. This method should update the fontSize data property, ensuring that the font size stays within the range of 12px to 48px.
In the <p> element with the class text-content, use the :style directive to dynamically set the font size and line height based on the fontSize data property.
By following these steps, you have completed the implementation of the e-book reader features, including the toggle of the header toolbar's visibility, the setting of the reading theme, and the setting of the font size.
Next, click on Go Live button in the bottom right corner of WebIDE, to run the project. Open "Web 8080" on the top of the VM and refresh it manually and you will see the page.
We use cookies for a number of reasons, such as keeping the website reliable and secure, to improve your experience on our website and to see how you interact with it. By accepting, you agree to our use of such cookies. Privacy Policy