lapots
lapots

Reputation: 13395

code does not work outside jsfiddle

There is a jsfiddle code that I'd like to use on my page.

I copied css and put it into <style> tag on my page. Then I separate part that starts with

$(function(){
    $('.anyClass').liEqualizer({

and put it into custom.js. And the first part that starts with (function ($) { I put into audio_frequency.js. I added its imports to head tag. The page looks like this

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <style>
            .sampleWrap { 
                margin-left: 6%;
                margin-top: 5%;
            }
            .eqCol {
                width: 80px;
                margin: 0 0 0 2px;
                float: left;
            }
            .eqItem { 
                height: 20px; 
                width: 100%; 
                background: transparent; 
                margin: 1px 0 0 0; 
                opacity: 0;
                box-shadow: 15px 20px 0px rgba(0,0,0,0.1);
            }
            .eqCol .eqItem:last-child {
                opacity:1 !important
            }
        </style>
        <script src="jquery-3.2.1.slim.min.js"></script>
        <script src="audio_frequency.js"></script>
        <script src="custom.js"></script>
    </head>
    <body>
        <div class="sampleWrap d-flex">
            <div class="anyClass"></div>
                <div style="clear:both; padding:15px 0">
                    <button class="start">start</button>
                    <button class="stop">stop</button>
               </div>
            </div>
        </div>
    </body>
</html>

custom.js looks like this

$(document).ready(function() {
    $('.anyClass').liEqualizer({
        row:7,          
        col:20,         
        speed:20,       
        freq:400,       
        on:true 
    });
    $('.start').click(function(){
        $('.anyClass').liEqualizer('start');
        return false;   
    })
    $('.stop').click(function(){
        $('.anyClass').liEqualizer('stop');
        return false;   
    })
});

and audio_frequency looks like this

(function ($) {
    var methods = {
        init: function (options) {
            var p = {
                row: 7,         
                col: 6,         
                speed: 20,      
                freq: 400,  
                on: true        
            };
            if (options) {
                $.extend(p, options); 
            }
            var eqWrap = $(this).addClass('eqWrap');
            for (c = 0; c < p.col; c++) {
                var eqColEl = $('<div>').addClass('eqCol').appendTo(eqWrap); 
                for(r = 0; r < p.row; r++){
                    $('<div>').addClass('eqItem').appendTo(eqColEl);    
                }
            }
            var 
            eqCol = $('.eqCol', eqWrap),
            eqItem = $('.eqItem', eqWrap),
            randomNumber = function (m, n){
                m = parseInt(m);
                n = parseInt(n);
                return Math.floor(Math.random() * (n - m + 1)) + m;
            },
            eqUp = function(colEl, val) {
                var 
                    speed = p.speed,
                    v = p.row - val,
                    i = p.row,
                    j = 0,
                    flag2 = true,
                    eachItemUp = function(){
                        $('.eqItem', colEl).eq(i - 1).nextAll().stop().css({ opacity:'1' });
                        if ($('.eqItem', colEl).eq(i - 1).css('opacity') == 1) { flag2 = false } 
                        else { flag2 = true }
                        $('.eqItem', colEl).eq(i - 1).stop(true).animate({ opacity:'1' }, p.speed, function() {
                            if ($('.eqItem', colEl).index(this) == v) {
                                if(flag2) {
                                    eqDown(colEl,val);
                                }
                            } else {
                                i--;
                                j++;
                                if(i>v){
                                    eachItemUp()    
                                }
                            }
                        })  
                    }
                eachItemUp()
            },
            eqDown = function(colEl,val){
                var 
                    v = p.row - val,
                    i = (p.row-val),
                    j = 0,
                    speed = p.speed * 2,
                    eachItemDown = function(){
                        if (i == (p.row - val)) {
                            $('.eqItem', colEl).eq(i).animate({ opacity:'0' }, speed * 10)
                            setTimeout(function() {
                                i++;
                                j++;
                                if(i < p.row){
                                    eachItemDown();
                                }       
                            }, speed)
                        } else {
                            $('.eqItem', colEl).eq(i).animate({ opacity:'0' }, speed, function(){
                                i++;
                                j++;
                                if(i < p.row){
                                    eachItemDown();
                                }   
                            })
                    }
                }
                eachItemDown();
            },
            eqInterval = function(){
                eqCol.each(function(){
                    eqUp($(this), randomNumber(0, p.row))   
                })
            }
            eqInterval()
            if (p.on) {
                var eqIntervalId = setInterval(eqInterval, p.freq)
                $(this).data({
                    'eqIntId': eqIntervalId,
                    'eqInt': eqInterval,
                    'freq': p.freq,
                    'on': p.on
                })
            } else {
                $(this).data({
                    'eqIntId':eqIntervalId,
                    'eqInt':eqInterval,
                    'freq':p.freq,
                    'on':p.on
                })
            }
        }, start: function () {
             if (!$(this).data('on')) {
                $(this).data('eqInt')();
                var eqIntervalId = setInterval($(this).data('eqInt'), $(this).data('freq'));
                $(this).data ({
                    'eqIntId':eqIntervalId,
                    'on':true
                })
            }
        },
        stop: function () {
            if($(this).data('on')) {
                clearInterval($(this).data('eqIntId'));
                $('.eqItem', $(this)).animate({opacity:0})
                $(this).data({
                    'on':false
                })
            }
        }
    };
    $.fn.liEqualizer = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' in jQuery.liEqualizer does not exist');
        }
    };
})(jQuery); 

But when I load page I get

TypeError: $(...).eq(...).nextAll(...).stop is not a function[Learn More]

What is the problem? Is it trying to evaluate something before all the components are ready on the page?

Upvotes: 0

Views: 63

Answers (1)

ferhado
ferhado

Reputation: 2594

This $(...).eq(...).nextAll(...).stop error is because of jquery-3.2.1.slim.min.js this ist not complete.

Note:
In the jquery.slim.js, the following functions of code are removed:

  1. jQuery.fn.extend
  2. jquery.fn.load
  3. jquery.each // Attach a bunch of functions for handling common AJAX events
  4. jQuery.expr.filters.animated
  5. ajax settings like jQuery.ajaxSettings.xhr, jQuery.ajaxPrefilter, jQuery.ajaxSetup, jQuery.ajaxPrefilter, jQuery.ajaxTransport, jQuery.ajaxSetup
  6. xml parsing like jQuery.parseXML,
  7. animation effects like jQuery.easing, jQuery.Animation, jQuery.speedIn the jquery.slim.js, the following function of code are removed:

Here is the complete code, you need jquery lib
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>

<style>
    /*Layout css*/
  
  body {
    margin: 0;
    padding: 20px 10px;
    text-align: center
  }
  
  .sampleWrap {
    height: 290px
  }
  /*plugin css*/
  
  .eqWrap {
    margin: -1px 0 0 -2px;
    overflow: hidden;
    display: inline-block; //display:inline; //zoom:1;}
    .eqCol {
      width: 37px;
      margin: 0 0 0 2px;
      float: left;
    }
    .eqItem {
      height: 10px;
      width: 100%;
      background: #e7aa3b;
      margin: 1px 0 0 0;
      opacity: 0
    }
    .eqCol .eqItem:last-child {
      opacity: 1 !important
    }
</style>
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>
<div class="sampleWrap">
  <div class="anyClass"></div>
  <div style="clear:both; padding:15px 0">
    <button class="start">start</button>
    <button class="stop">stop</button>
  </div>

  <div class="anyClass2"></div>
  <div style="clear:both; padding:15px 0">
    <button class="start2">start</button>
    <button class="stop2">stop</button>
  </div>
</div>
<script>
  /*код плагина*/
  (function($) {
    var methods = {
      init: function(options) {
        var p = {
          row: 7, //кол-во столбцов
          col: 6, //кол-во колонок
          speed: 20, //скорость подсветки кубиков
          freq: 400, //частота сигнала
          on: true //включено по умолчанию (true,false)
        };
        if (options) {
          $.extend(p, options);
        }
        var eqWrap = $(this).addClass('eqWrap');
        for (c = 0; c < p.col; c++) {
          var eqColEl = $('<div>').addClass('eqCol').appendTo(eqWrap);
          for (r = 0; r < p.row; r++) {
            $('<div>').addClass('eqItem').appendTo(eqColEl);
          }
        }
        var
          eqCol = $('.eqCol', eqWrap),
          eqItem = $('.eqItem', eqWrap),
          randomNumber = function(m, n) {
            m = parseInt(m);
            n = parseInt(n);
            return Math.floor(Math.random() * (n - m + 1)) + m;
          },
          eqUp = function(colEl, val) {
            var
              speed = p.speed,
              v = p.row - val,
              i = p.row,
              j = 0,
              flag2 = true,
              eachItemUp = function() {
                $('.eqItem', colEl).eq(i - 1).nextAll().stop().css({
                  opacity: '1'
                });
                if ($('.eqItem', colEl).eq(i - 1).css('opacity') == 1) {
                  flag2 = false
                } else {
                  flag2 = true
                }
                $('.eqItem', colEl).eq(i - 1).stop(true).animate({
                  opacity: '1'
                }, p.speed, function() {
                  if ($('.eqItem', colEl).index(this) == v) {
                    if (flag2) {
                      eqDown(colEl, val);
                    }
                  } else {
                    i--;
                    j++;
                    if (i > v) {
                      eachItemUp()
                    }
                  }
                })

              }
            eachItemUp()
          },
          eqDown = function(colEl, val) {
            var
              v = p.row - val,
              i = (p.row - val),
              j = 0,
              speed = p.speed * 2,
              eachItemDown = function() {
                if (i == (p.row - val)) {
                  $('.eqItem', colEl).eq(i).animate({
                    opacity: '0'
                  }, speed * 10)
                  setTimeout(function() {
                    i++;
                    j++;
                    if (i < p.row) {
                      eachItemDown();
                    }
                  }, speed)
                } else {
                  $('.eqItem', colEl).eq(i).animate({
                    opacity: '0'
                  }, speed, function() {
                    i++;
                    j++;
                    if (i < p.row) {
                      eachItemDown();
                    }
                  })
                }
              }
            eachItemDown();
          },
          eqInterval = function() {
            eqCol.each(function() {
              eqUp($(this), randomNumber(0, p.row))
            })
          }
        eqInterval()
        if (p.on) {
          var eqIntervalId = setInterval(eqInterval, p.freq)
          $(this).data({
            'eqIntId': eqIntervalId,
            'eqInt': eqInterval,
            'freq': p.freq,
            'on': p.on
          })
        } else {
          $(this).data({
            'eqIntId': eqIntervalId,
            'eqInt': eqInterval,
            'freq': p.freq,
            'on': p.on
          })
        }
      },
      start: function() {
        if (!$(this).data('on')) {
          $(this).data('eqInt')();
          var eqIntervalId = setInterval($(this).data('eqInt'), $(this).data('freq'));
          $(this).data({
            'eqIntId': eqIntervalId,
            'on': true
          })
        }
      },
      stop: function() {
        if ($(this).data('on')) {
          clearInterval($(this).data('eqIntId'));
          $('.eqItem', $(this)).animate({
            opacity: 0
          })
          $(this).data({
            'on': false
          })
        }
      }
    };
    $.fn.liEqualizer = function(method) {
      if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
      } else if (typeof method === 'object' || !method) {
        return methods.init.apply(this, arguments);
      } else {
        $.error('Метод ' + method + ' в jQuery.liEqualizer не существует');
      }
    };
  })(jQuery);
  /*инициализация плагина*/
  $(function() {
    $('.anyClass').liEqualizer({
      row: 7, //кол-во столбцов
      col: 6, //кол-во колонок
      speed: 20, //скорость подсветки кубиков
      freq: 400, //частота сигнала
      on: true //включено по умолчанию (true,false)
    });
    $('.start').click(function() {
      $('.anyClass').liEqualizer('start');
      return false;
    })
    $('.stop').click(function() {
      $('.anyClass').liEqualizer('stop');
      return false;
    })

    $('.anyClass2').liEqualizer({
      row: 7, //кол-во столбцов
      col: 6, //кол-во колонок
      speed: 20, //скорость подсветки кубиков
      freq: 400, //частота сигнала
      on: false //включено по умолчанию (true,false)
    });
    $('.start2').click(function() {
      $('.anyClass2').liEqualizer('start');
      return false;
    })
    $('.stop2').click(function() {
      $('.anyClass2').liEqualizer('stop');
      return false;
    })
  });
</script>

Upvotes: 1

Related Questions