简体   繁体   English

RAILS / HTML:每7秒钟更改一次图片幻灯片

[英]RAILS/HTML: Change image slide every 7 seconds

I've seen a lot of questions like mine here in stack just like these three: 1 , 2 and 3 . :我在栈就像这三个在这里看到了很多像我这样的问题, 123 But I tried their way but it didn't work. 但是我尝试了他们的方法,但是没有成功。

I am looking for a more simple way. 我正在寻找一种更简单的方法。 Using Javascript in-line in html, I tried to have this code (which I found in one of those questions here in stack) 在html中使用内联Javascript,我尝试使用此代码(在堆栈中这些问题之一中找到了该代码)

<script>
function FetchData() {
  $("#pack").animate({
      left: '-1000px'
  }, 'slow');
}
setTimeout(FetchData, 7000); </script>

and have this as well... 并有这个...

<img id="pack" src="image.jpg">

but it seems to not work. 但似乎不起作用。 Nothing happened. 没啥事儿。 I have looked and stared at the photo for over a minute and it did not move at all. 我看了一下,盯着照片看了一分钟,照片完全没有动。 What was wrong? 什么问题? Do I lack some codes? 我缺少一些代码吗? I have no css code that relates to this by the way. 我没有与此相关的CSS代码。

When your teacher says "separate js and html", and that you shouldn't use inline javascript, they probably mean that you should have something like this setup: 当您的老师说“ js和html分开”,并且您不应该使用内联javascript时,它们可能意味着您应该具有以下设置:

in your layout, include a javascript file (this might be included by default). 在您的布局中,添加一个javascript文件(默认情况下可能会包含)。 It's best to put these down the bottom. 最好将这些放到最下面。 If there's a default layout it might have a place for javascript includes already. 如果有默认布局,则可能已经有一个包含javascript的地方。

<%= javascript_include_tag "application" %>

in your view 在你看来

<img id="pack" src="image.jpg">

in application.js 在application.js中

$(function() {
  //run on dom ready
  console.log("about to setTimeout");
  setTimeout(FetchData, 7000);
  console.log("done setTimeout");
});

function FetchData() {
  console.log("in FetchData, about to animate, $('#pack').size() = "+$('#pack').size());
  $("#pack").animate({
      left: '-1000px'
  }, 'slow');
}

Try to use carousel using bootstrap and put this on the topmost part of your code: 尝试通过引导程序使用轮播,并将其放在代码的最顶部:

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

This is the code for the slides in html: 这是html中幻灯片的代码:

            <div class="carousel slide" id="myCarousel" data-ride="carousel">
                <ol class="carousel-indicators">
                    <li class="active" data-slide-to="0" data-target="#myCarousel">
                    </li>
                    <li data-slide-to="1" data-target="#myCarousel">
                    </li>
                    <li data-slide-to="2" data-target="#myCarousel">
                    </li>
                </ol>
                <div class="carousel-inner">
                    <div class="item active">
                        <%= image_tag 'image_1.jpg' %>
                    </div>
                    <div class="item">
                        <%= image_tag 'image_2.jpg' %>
                    </div>
                    <div class="item">
                        <%= image_tag 'image_3.jpg' %>
                    </div>
                </div>
                <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left"></span>
                </a>
                <a class="right carousel-control" href="#myCarousel" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                </a>
            </div>

and this is the javascript in-line code: 这是javascript内联代码:

<script type="text/javascript">
    $(document).ready(function(){
        $("#myCarousel").carousel({
            interval : 7000,
            pause: false
        });
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM