EduardET

article cards with custom button

Nov 15th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function ($) {
  2.   if (!$('.divi-100-article-card').length) {
  3.     return false;
  4.   } else {
  5.     $('.et_pb_blog_grid').find('.et_pb_post').each(function() {
  6.       var $this = $(this);
  7.  
  8.       /**
  9.        * Adds article-card class to div
  10.        */
  11.       $this
  12.         .addClass('article-card');
  13.  
  14.       /**
  15.        * Creates content div and appends to post
  16.        */
  17.       $this
  18.         .append('<div class="article-card__content" />');
  19.  
  20.       /**
  21.        * Post variables
  22.        */
  23.       var postContent = $this.find('.article-card__content');
  24.       var postMeta = $this.children('.post-meta');
  25.       var buttonLink = $this.find('.entry-title a').attr('href');
  26.       var button = $('<a href="'+buttonLink+'" class="wpc-button">Zum Angebot</a>');
  27.  
  28.       /**
  29.        * Excerpt variables
  30.        */
  31.       var excerpt = $this.clone().children().remove().end().text().trim();
  32.       var excerptWrap = $('<div class="article-card__excerpt" />');
  33.  
  34.       if ($this.children('p').not('p.post-meta').length > 0) {
  35.         excerpt = $this.children('p').not('p.post-meta').text();
  36.         $this.children('p').not('p.post-meta').remove();
  37.       }
  38.  
  39.       /**
  40.        * Check if excerpt is over 100 characters
  41.        */
  42.       if (excerpt.length > 80) {
  43.         excerpt = excerpt.substring(0, 80).split(" ").slice(0, -1).join(" ") + "..."
  44.       }
  45.  
  46.       /**
  47.        * Creates category div based off post-meta children
  48.        */
  49.       postMeta
  50.         .children('a')
  51.         .addClass('article-card__category')
  52.         .appendTo(postContent);
  53.    
  54.       button.appendTo(postContent);
  55.      
  56.       /**
  57.        * Appends excerpt to content div
  58.        */
  59.       excerptWrap
  60.         .text(excerpt)
  61.         .appendTo(postContent);
  62.  
  63.       /**
  64.        * Creates meta div and appends to content
  65.        */
  66.       postContent
  67.         .append('<div class="article-card__meta" />');
  68.  
  69.       /**
  70.        * Removes old text from post
  71.        */
  72.       $this
  73.         .contents()
  74.         .filter(function () {
  75.           return (this.nodeType == 3);
  76.         })
  77.         .remove();
  78.  
  79.       /**
  80.        * Creates date div based off .published
  81.        */
  82.       $(this).find('.published').text(function() {
  83.         return $(this).text().slice(0, -6);
  84.       });
  85.  
  86.       $this
  87.         .find('.published')
  88.         .appendTo(this)
  89.         .replaceWith(function (i, text) {
  90.           return (
  91.             text
  92.               .replace(/([a-zA-Z]+)([\d\D]*)/g,
  93.                 '<div class="article-card__date">\
  94.                  <span class="article-card__day">$2</span>\
  95.                  <span class="article-card__month">$1</span>\
  96.                </div>'
  97.               )
  98.           );
  99.         });
  100.  
  101.         /**
  102.          * Removes comma, spaces from day
  103.          */
  104.         $this
  105.           .find('.article-card__day')
  106.           .text(function() {
  107.             return $(this)
  108.                     .text()
  109.                     .replace(/\,/g, '')
  110.                     .trim();
  111.           });
  112.  
  113.       /**
  114.        * Add article-card__title class to title
  115.        */
  116.       $this
  117.         .find('.entry-title')
  118.         .addClass('article-card__title')
  119.         .prependTo(postContent);
  120.  
  121.       /**
  122.        * Appends author to content div
  123.        */
  124.       $this
  125.         .find('.author')
  126.         .addClass('article-card__author')
  127.         .appendTo($this.find('.article-card__meta'));
  128.  
  129.  
  130.       /**
  131.        * Get existing comment and appends it to post comment
  132.        */
  133.       var comments = postMeta.text().replace(/[^a-zA-Z0-9 ]/g, "").replace("by", "").trim();
  134.  
  135.       if (comments) {
  136.         $('<span class="article-card__comments">' + comments + '</span>')
  137.           .appendTo($this.find('.article-card__meta'));
  138.       }
  139.  
  140.       /**
  141.        * Remove old post-meta div
  142.        */
  143.       postMeta.remove();
  144.  
  145.       /**
  146.        * If .post-content exist, then append contents to excerpt
  147.        */
  148.       if ($(this).find('.post-content').length > 0) {
  149.         $(this)
  150.           .find('.post-content p')
  151.           .appendTo($(this)
  152.           .find('.article-card__excerpt'));
  153.       }
  154.  
  155.       /**
  156.        * Hide excerpt by default on desktop
  157.        */
  158.       if ($(window).width() > 768) {
  159.         $this
  160.           .children()
  161.           .children('.article-card__excerpt')
  162.           .hide();
  163.       }
  164.  
  165.       /**
  166.        * Get outer height of content div and applies a padding to card
  167.        */
  168.       function postModuleSize() {
  169.         var postContentHeight = postContent.outerHeight();
  170.  
  171.         $this.css({
  172.           'padding-bottom': postContentHeight
  173.         });
  174.       }
  175.  
  176.       setTimeout(postModuleSize, 100);
  177.  
  178.       /**
  179.        * Recall getPostContentSize() on window resize
  180.        */
  181.       $(window).resize(function() {
  182.         postModuleSize();
  183.       });
  184.  
  185.       /**
  186.        * Handle animations on desktop
  187.        */
  188.       if ($(window).width() > 768) {
  189.  
  190.         /**
  191.          * Prevents loading incorrect state
  192.          */
  193.         setTimeout(function() {
  194.           $this.on('hover', function () {
  195.             $this.find('.article-card__excerpt').stop().animate({
  196.               height: "toggle",
  197.               opacity: "toggle"
  198.             }, 200);
  199.           });
  200.         });
  201.       }
  202.     });
  203.   }
  204. });
Advertisement
Add Comment
Please, Sign In to add comment