MrBoBo69

Read more & Read less (shopify)

Dec 23rd, 2024
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.84 KB | Source Code | 0 0
  1. <div id="truncated">
  2.   {{ product.description | truncatewords: 50, "... <a class='read-more-collection'>Read More</a>" }}
  3. </div>
  4. <div id="fullDescription" style="display: none; opacity: 0;">
  5.   {{ product.description }}
  6.   <a id="readLess" class="read-less-collection" style="cursor: pointer; font-weight: bold;">Read Less</a>
  7. </div>
  8.  
  9.               {%- endif -%}
  10.             {%- endif -%}
  11. <style>
  12.  .read-more-collection:hover{cursor:pointer}
  13. </style>
  14.  
  15. <script>
  16. document.getElementById('truncated').addEventListener('click', function () {
  17.   const truncated = document.getElementById('truncated');
  18.   const fullDescription = document.getElementById('fullDescription');
  19.  
  20.   // Fade out the truncated description
  21.   truncated.style.transition = 'opacity 0.3s';
  22.   truncated.style.opacity = '0';
  23.  
  24.   // Wait for the fade-out to complete
  25.   setTimeout(() => {
  26.     truncated.style.display = 'none'; // Hide the truncated element
  27.     fullDescription.style.display = 'block'; // Show the full description
  28.  
  29.     // Fade in the full description
  30.     fullDescription.style.transition = 'opacity 0.5s';
  31.     fullDescription.style.opacity = '1';
  32.   }, 300);
  33. });
  34.  
  35. document.getElementById('readLess').addEventListener('click', function () {
  36.   const truncated = document.getElementById('truncated');
  37.   const fullDescription = document.getElementById('fullDescription');
  38.  
  39.   // Fade out the full description
  40.   fullDescription.style.transition = 'opacity 0.3s';
  41.   fullDescription.style.opacity = '0';
  42.  
  43.   // Wait for the fade-out to complete
  44.   setTimeout(() => {
  45.     fullDescription.style.display = 'none'; // Hide the full description
  46.     truncated.style.display = 'block'; // Show the truncated description
  47.  
  48.     // Fade in the truncated description
  49.     truncated.style.transition = 'opacity 0.5s';
  50.     truncated.style.opacity = '1';
  51.   }, 300);
  52. });
  53.  
  54.  
  55. </script>
  56.  
Advertisement
Add Comment
Please, Sign In to add comment