【问题标题】:Javascript / HTML banner not workingJavascript / HTML横幅不起作用
【发布时间】:2014-04-03 13:50:06
【问题描述】:

我正在关注本教程(已删除链接,因为 SO 上不允许使用 YouTube 视频)。

我不明白为什么横幅不能按原样闪烁,我有 4 张图像我打算让它闪烁。

另外,如果有人确实解决了这个问题,我怎样才能使图像链接到另一个页面,例如,是否像在 HTML 代码中添加 <a href=.......> 一样简单?

HTML:

<script type="text/javascript" src:"jquery.js"></script>
<div id="banner">
    <img src="images/banner1.jpg" class="active" />
    <img src="images/banner4.jpg" />
    <img src="images/banner2.jpg" />
    <img src="images/banner3.jpg" />
</div>

javascript/jQuery

​​>
    $(document).ready(function () {
    setInterval(function () {
        //Get current active image
        var active = $('#banner .active');

        // If there is another image(object) left then make that image next
        // If not, go back to the first image of the banner div
        if (active.next().length > 0) var next = active.next();
        else var next = $('#banner img:first');

        //Get the next image ready by modifying the z-index
        next.css('z-index', '2');

        //Fade out the active image, then
        active.fadeOut(1000, function () {

        });
        //Move the active image to the back of the pile, show it and             remove     the active class
        active.css('z-index', '1').show().removeClass('active');
        //Make the next image the active one    
        next.css('z-index', '3').addClass('active');
    });

}, 3000);
}); 

CSS

#banner {
    position: relative;
    margin-left: auto;
    margin-right: auto;
    height: 350px;
    width: 950px;
    margin-top: 10px;
}
#banner img {
    position:absolute;
    z-index:1;
}
#banner img.active {
    z-index:3;
}

【问题讨论】:

  • src:"jquery.js" 应该是src="jquery.js"
  • 已纠正但未修复,是在搜索 jquery 文件吗?我在构建了一半的主页后添加了这个,所以我需要添加一个“jquery.js”文件还是它是如何工作的?
  • 您将jquery.js 放在与您的主页相同的位置,该链接将起作用。您的 jQuery 部分还必须有很多 }。我会回到教程并找出你哪里出错了。还有一件事,我建议不要使用视频教程与文字相比可能很难理解。
  • 如果你让它工作,这就是它的样子DEMO HERE 那就是你的代码在工作。 (我使用不同尺寸的图像只是为了表明它可以正常工作,您可以使用相同的尺寸。)
  • 谢谢,它还说第二行到最后一行的语法错误(上面的行

标签: html css rotation banner


【解决方案1】:

所以首先你没有链接你的脚本。

你有:

<script type="text/javascript" src:"jquery.js"></script>

应该是:

<script type="text/javascript" src="jquery.js"></script>

正如您刚刚输入的jquery.js,这意味着如果您的主页(使用它的任何页面)在C:\Users\Me\Documents\Website 中,那么jquery.js 也需要在同一个文件夹中。


然后我们转到 jQuery,它一切正常,直到它结束。你用}, 3000);关闭它 但然后尝试使用}); 再次关闭它。缩进也暴露了它。

所以当我们修复它时,我们会得到这个DEMO HERE

【讨论】:

  • 感谢您的帮助,我已经更正了这些,但仍然无法正常工作!
  • 没什么...我修好了。因此它的工作演示。尝试改用&lt;script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"&gt;&lt;/script&gt;。我猜你只是没有链接文件。所以使用网络版。
  • @user3086732 也不要更改您的 OP,它可以帮助其他人找到类似的问题。
  • 感谢红润!在计时之前代码缩进结束所以时间没有计算并导致疯狂的闪烁循环!非常感谢完美的答案和快速响应!
  • @user3086732 将3000(3 秒)更改为其他内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 2017-03-27
  • 2011-07-04
  • 2021-11-25
相关资源
最近更新 更多