This tutorial describes how to use Velo to let site visitors expand and collapse text with "Read More" and "Read Less" buttons.
To create Read More / Read Less functionality, we use a multi-state box. Multi-state boxes are great for switching between several views. They contain multiple states with different content, and display one state at a time.
One state in our multi-state box will contain the shorter (collapsed) content and one state will contain the longer (expanded) content. We'll use code to enable site visitors to switch between the 2 states by clicking "Read More" and "Read Less" buttons.
To add a multi-state box to your page:
Wix Studio:
Wix Editor:
Click on the pre-designed multi-state box and update the design as follows:
Notes:
When you click your multi-state box, you can see the ID (name) of the multi-state box in the Properties & Events panel. When you click Manage States, you can see the IDs of the default states for the multi-state box.
You can rename both your multi-state box and your states in the Properties & Events panel. It's a good idea to give your IDs meaningful names, since you'll be using them in code.
We renamed our multi-state box to readMoreStatebox
and our state to collapsedState
, since this state will have the collapsed version of the text.
Now you can add your page content to your state: images, videos, text, or other elements. Make sure the elements fit within the borders of your multi-state box so they'll be attached to the state.
Since this is the collapsed state, add the short version of your text.
When you're finished setting up your state, do the following:
readMoreButton
in the Properties & Events panel.Now you can duplicate your collapsed state and then adjust it to create the expanded state:
Note You can switch between your states by clicking Manage States and selecting the state you want to edit.
expandedState
in the Properties & Events panel.readLessButton
in the Properties & Events panel.Now we need to write code to define when to switch between the collapsed state and expanded state. We use the MultiStateBox API to define when to switch states.
Open the code panel.
Add the code in lines 2-8 below to your onReady()
function so that the code on your page looks like this:
readMoreStatebox
multi-state box to the expandedState
.readMoreStatebox
multi-state box to the collapsedState
.Preview your site to make sure everything is working as expected. Then go ahead and publish.