Advertisement
Guest User

FindNext Tag - Jquery

a guest
Jan 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.04 KB | None | 0 0
  1. $.fn.findNext = function(selector, steps, scope)
  2. {
  3.     // Steps given? Then parse to int
  4.     if (steps)
  5.     {
  6.         steps = Math.floor(steps);
  7.     }
  8.     else if (steps === 0)
  9.     {
  10.         // Stupid case :)
  11.         return this;
  12.     }
  13.     else
  14.     {
  15.         // Else, try the easy way
  16.         var next = this.next(selector);
  17.         if (next.length)
  18.             return next;
  19.         // Easy way failed, try the hard way :)
  20.         steps = 1;
  21.     }
  22.  
  23.     // Set scope to document or user-defined
  24.     scope = (scope) ? $(scope) : $(document);
  25.  
  26.     // Find kids that match selector: used as exclusion filter
  27.     var kids = this.find(selector);
  28.  
  29.     // Find in parent(s)
  30.     hay = $(this);
  31.     while(hay[0] != scope[0])
  32.     {
  33.         // Move up one level
  34.         hay = hay.parent();    
  35.         // Select all kids of parent
  36.         //  - excluding kids of current element (next != inside),
  37.         //  - add current element (will be added in document order)
  38.         var rs = hay.find(selector).not(kids).add($(this));
  39.         // Move the desired number of steps
  40.         var id = rs.index(this) + steps;
  41.         // Result found? then return
  42.         if (id > -1 && id < rs.length)
  43.             return $(rs[id]);
  44.     }
  45.     // Return empty result
  46.     return $([]);
  47. }
  48.    
  49.    
  50. $(".card-title").each(function() {
  51.    var cartTitleHieght = $(this).height();
  52.    var cartDestHieght = $(this).findNext('.card-dest').height();
  53. //  console.log('title: ' + cartTitleHieght);
  54. //  console.log('dest: ' + cartDestHieght);
  55.     descLen = $(this).findNext('.max-text').text().length;
  56.     if(cartTitleHieght + cartDestHieght <= 48) {
  57.        $(this).findNext(".max-text").text($(this).findNext(".max-text").text().substr(0,200)+'...');
  58.        } else if (cartTitleHieght + cartDestHieght <= 72) {
  59.          $(this).findNext(".max-text").text($(this).findNext(".max-text").text().substr(0,130)+'...');
  60.                  
  61.          } else if (cartTitleHieght + cartDestHieght <= 96) {
  62.                      $(this).findNext(".max-text").text($(this).findNext(".max-text").text().substr(0,80)+'...');
  63.                     }
  64.  
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement