In this tutorial, I will explain how to use custom block types to create a Parallax effect in your Drupal 8 subtheme.
To follow along with these steps, it would help to have a good understanding of Drupal 8 theming. You can click here to take our Drupal 8 Theming Class.
To be able to follow this tutorial, you also need to install Bootstrap and create a subtheme from the CDN folder. You can follow our guide here in our earlier post.
Step #1. Configure the Bootstrap theme
Parallax effects are usually full width so you need to turn on Fluid container option from the Bootstrap theme menu.
- Go to "Appearance" and make sure the Bootstrap theme is set as default.
- Enter the Bootstrap theme settings and select the Container drop-down.
- Check the box for "Fluid container".
- Click "Save configuration".
Step #2. Create blocks
Now you need to make some custom blocks for the display. By using custom blocks you can theme the blocks specifically.
- Go to Structure > Block layout > Types.
- Click "Add custom block type".
- Label the custom block "Parallax":
- Now go to "Add custom block" and create two blocks, one for each of the images you are using. I called these blocks Parallax 1 and Parallax 2.
- Set the region to content and save the blocks.
Now you need to use the custom basic blocks to display content between the images on the block layout page. Also, remove any blocks you have placed in the sidebars as this will prevent the width being 100%.
Step #3. Enable debugging
- Now turn on debugging if you don't already have it enabled.
- Go to sites/default and copy and rename default.services.yml to services.yml on line 58 change debug to true:
- Now, to be able to see the theme suggestions, add this code to your subtheme.theme file:
- The function name needs to match your theme name. Adding this to your theme file allows you to theme custom blocks.
- Flush all caches and if you inspect the site now you should see theme suggestions.
- In Google Chrome, right click on the region and select Inspect to access the inspector tool:
- Add the template suggestion for parallax block in your templates theme folder subtheme/templates/block--bundle--parallax.html.twig
- Add this code to the template file:
- This code will style the parallax custom block you created. If you need to debug this you can use kint from the Devel module.
Step #4. Style the blocks
Now you need to add the CSS to style the blocks.
- Open your /css/style.css and add this code:
- You will need to update .block-block-content2b3d7746-776f-484c-8686-c0e00a3978b6 to match your block:
This is the class assigned to your custom block. If you named your Parallax block differently, you will have to update .parallax--parallax1 and .parallax--parallax2.
Step #5. Add the images and ScrollMagic
- Go to your theme and create a folder called /images/. This is where you will save the images for the parallax effect.
- Now go to your theme and create a folder called /js/ and move these files into the folder
- Now you need to add our custom js add parallax.js
- You now need to tell the theme to load the js libraries. To do that, open subtheme/subtheme.libraries.yml:
- And finally, add this code to your parallax.js file:
- Make sure the images you are using are big enough for the max width you want to display.
- And now you should have a nice scrolling effect that you have applied only to our custom blocks: