jQuery: HTML显示为空,没有错误,可能是头部错误。

huangapple go评论65阅读模式
英文:

jQuery: html shows nothing with no error, maybe head error?

问题

I understand that you want a translation for the provided text, excluding the code. Here's the translated text:

"抱歉,我的英文不太好,这是我的第二语言。

我正试图修复来自CodePen的使用jQuery的代码。这是一个自动滑动的轮播代码。但是,当我尝试将这些代码移到我的自己的文件时,我的HTML不显示轮播。控制台没有错误消息。

这是我的HTML头部:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="main.css" />
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.1.0/velocity.min.js"></script>
    <script src="main.js"></script>
</head>

这是我尝试使用的JavaScript文件(main.js):

var sliderTeam = (function(document, $) {
    'use strict';

    // ...(这里是代码部分)

    return {
        init: _init
    };
})(document, jQuery);

$(window).load(function(){
    'use strict';
    sliderTeam.init();
});

这是CodePen链接:
https://codepen.io/fixcl/pen/KwpKvb

我是否添加了错误版本的JavaScript文件?还是我漏掉了其他东西?

编辑:
main.js已连接到HTML文件。我可以使用以下代码检查main.js文件:

console.log($current);

编辑2:这是我的HTML文件的主体部分:

<body>
    <div class="slider--teams">
        <div class="slider--teams__team">
            <ul id="list" class="cf">
                <li>
                    <figure class="active">
                        <figcaption>
                            <img src="https://odium.kr/assets/css/images/symbol-cernium.png">
                            <h2>Billie</h2>
                            <p>Team Head</p>
                        </figcaption>
                    </figure>
                </li>

                <li>
                    <figure>
                        <figcaption>
                            <img src="https://odium.kr/assets/css/images/symbol-arcus.png">
                            <h2>Roger</h2>
                            <p>Art Director</p>
                        </figcaption>
                    </figure>
                </li>

                <li>
                    <figure>
                        <figcaption>
                            <img src="https://odium.kr/assets/css/images/symbol-odium.png">
                            <h2>Wendy</h2>
                            <p>Designer</p>
                        </figcaption>
                    </figure>
                </li>
            </ul>
        </div>
    </div>
</body>

希望这些信息有助于解决您的问题。

英文:

Sorry for poor English, it is my second language.

I am trying to fix a code from codepen that uses jQuery. It is a code for carousel that slides auto. However, when I try to move these code to my own file, my html does not show up carousel. There is no error on console log.
Here is my HTML header

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;main.css&quot; /&gt;
    &lt;script src=&quot;//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/velocity/1.1.0/velocity.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;main.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

This is the javascript file(main.js) that I am trying to use

var sliderTeam = (function(document, $) {
  
    &#39;use strict&#39;;
    
    var $sliderTeams = $(&#39;.slider--teams&#39;),
        $list = $(&#39;#list&#39;),
        $listItems = $(&#39;#list li&#39;),
        $nItems = $listItems.length,
        $nView = 3,
        autoSlider,
        $current = 0,
        $isAuto = true,
        $acAuto = 2500,
        
        _init = function() {
          _initWidth();
          _eventInit();
        },
        
        _initWidth = function() {
          $list.css({
            &#39;margin-left&#39;: ~~(100 / $nView) + &#39;%&#39;,
            &#39;width&#39;: ~~(100 * ($nItems / $nView)) + &#39;%&#39;
          });
          $listItems.css(&#39;width&#39;, 100 / $nItems + &#39;%&#39;);
          $sliderTeams.velocity({ opacity: 1 }, { display: &quot;block&quot; }, { delay:1000 });
        },
        
        _eventInit = function() {
          
          window.requestAnimFrame = (function() {
            return  window.requestAnimationFrame       || 
                window.webkitRequestAnimationFrame || 
                window.mozRequestAnimationFrame    || 
                window.oRequestAnimationFrame      || 
                window.msRequestAnimationFrame     || 
                function(callback, element){
                  window.setTimeout(callback, 1000 / 60);
                };
          })();
  
          window.requestInterval = function(fn, delay) {
              if( !window.requestAnimationFrame       &amp;&amp; 
                  !window.webkitRequestAnimationFrame &amp;&amp; 
                  !window.mozRequestAnimationFrame    &amp;&amp; 
                  !window.oRequestAnimationFrame      &amp;&amp; 
                  !window.msRequestAnimationFrame)
                      return window.setInterval(fn, delay);
              var start = new Date().getTime(),
              handle = new Object();
  
              function loop() {
                  var current = new Date().getTime(),
                  delta = current - start;
                  if(delta &gt;= delay) {
                      fn.call();
                      start = new Date().getTime();
                  }
                  handle.value = requestAnimFrame(loop);
              };
              handle.value = requestAnimFrame(loop);
              return handle;
          }
  
          window.clearRequestInterval = function(handle) {
              window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) :
              window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value)   :
              window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) :
              window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) :
              window.msCancelRequestAnimationFrame ? msCancelRequestAnimationFrame(handle.value) :
              clearInterval(handle);
          };
          
          $.each($listItems, function(i) {
            var $this = $(this);
            $this.on(&#39;touchstart click&#39;, function(e) {
              e.preventDefault();
              _stopMove(i);
              _moveIt($this, i);
            });
          });
          
          autoSlider = requestInterval(_autoMove, $acAuto);
        },
        
        _moveIt = function(obj, x) {
          
          var n = x;
          
          obj.find(&#39;figure&#39;).addClass(&#39;active&#39;);        
          $listItems.not(obj).find(&#39;figure&#39;).removeClass(&#39;active&#39;);
          
          $list.velocity({
            translateX: ~~((-(100 / $nItems)) * n) + &#39;%&#39;,
            translateZ: 0
          }, {
            duration: 1000,
            easing: [400, 26],
            queue: false
          });
          
        },
        
        _autoMove = function(currentSlide) {
          if ($isAuto) { 
            $current = ~~(($current + 1) % $nItems);
          } else {
            $current = currentSlide;
          }
          console.log($current);
          _moveIt($listItems.eq($current), $current);
        },
        
        _stopMove = function(x) {
          clearRequestInterval(autoSlider);
          $isAuto = false;
          _autoMove(x);
        };
    
    return {
      init: _init
    };
  
  })(document, jQuery);
  
  $(window).load(function(){
    &#39;use strict&#39;;
    sliderTeam.init();
  });

This is the codepen:
https://codepen.io/fixcl/pen/KwpKvb

Did I add the wrong version of js file? Or did I miss something else?

Edited:
main.js is connected to html file. I can check with the code from main.js

console.log($current);

Edited2: This is my body of html file

&lt;body&gt;
&lt;div class=&quot;slider--teams&quot;&gt;
    &lt;div class=&quot;slider--teams__team&quot;&gt;
      &lt;ul id=&quot;list&quot; class=&quot;cf&quot;&gt;
        &lt;li&gt;
          &lt;figure class=&quot;active&quot;&gt;
            &lt;figcaption&gt;
              &lt;img src=&quot;https://odium.kr/assets/css/images/symbol-cernium.png&quot;&gt;
              &lt;h2&gt;Billie&lt;/h2&gt;
              &lt;p&gt;Head of Team&lt;/p&gt;
            &lt;/figcaption&gt;
          &lt;/figure&gt;
        &lt;/li&gt;

        &lt;li&gt;
          &lt;figure&gt;
            &lt;figcaption&gt;
              &lt;img src=&quot;https://odium.kr/assets/css/images/symbol-arcus.png&quot;&gt;
              &lt;h2&gt;Roger&lt;/h2&gt;
              &lt;p&gt;Art Director&lt;/p&gt;
            &lt;/figcaption&gt;
          &lt;/figure&gt;
        &lt;/li&gt;

        &lt;li&gt;
          &lt;figure&gt;
            &lt;figcaption&gt;
                          &lt;img src=&quot;https://odium.kr/assets/css/images/symbol-odium.png&quot;&gt;
              &lt;h2&gt;Wendy&lt;/h2&gt;
              &lt;p&gt;Designer&lt;/p&gt;
            &lt;/figcaption&gt;
          &lt;/figure&gt;
        &lt;/li&gt;  
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;

答案1

得分: 1

请将main.js放在闭合标签之前,因为您需要在DOM元素准备就绪后开始执行main.js。
请查看plnkr以获取详细信息:
https://plnkr.co/edit/CuN7q7j6lrQE57DH?open=lib%2Fscript.js&amp;preview

<body>
...
<script src="main.js"></script>
</body>

英文:

you should put the main.js before body close tag, since you need to start execute main.js after DOM element is ready.
please check the plnkr for detail:
https://plnkr.co/edit/CuN7q7j6lrQE57DH?open=lib%2Fscript.js&amp;preview

&lt;body&gt;
...
&lt;script src=&quot;main.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

huangapple
  • 本文由 发表于 2023年2月8日 13:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定