简体   繁体   English

无法初始化动态构建的实体化轮播

[英]Trouble Initializing a dynamically built materialize carousel

I am attempting to dynamically build a Materialize carousel (NOT a slider) from Flickr pictures, but I haven't been able to get the pictures to change. 我试图从Flickr图片动态构建一个Materialise轮播(不是滑块),但我无法让图片改变。 The picture that shows is always the last picture taken from Flickr, so I feel like something is scrolling, but it just isn't continuing to rotate the way a carousel should. 显示的图片总是从Flickr拍摄的最后一张照片,所以我觉得有些东西在滚动,但它不会像旋转木马那样继续旋转。

I've looked on SO and Google for answers, but 99% of the info is Bootstrap specific. 我已经在SO和Google上寻找答案,但99%的信息都是特定于Bootstrap的。 I've added an active class to the first item and tried initializing the carousel both from inside the html as well as from the javascript, but neither seemed to help. 我已经在第一个项目中添加了一个活动类,并尝试从html内部和javascript中初始化轮播,但似乎都没有帮助。 Here is the html code: 这是html代码:

<div class="carousel initialized" id="flickrPic"></div>

and the JS: 和JS:

$.ajax({
 type: "GET",
 url: flickrApiUrl + $.param(flickrApiParams),
 success: function(response) {
    var flickrPetPics = response.photos.photo
    for(i=0; i<flickrPetPics.length; i++) {
      var newSlide = makeFlickrCarousel(flickrPetPics[i]);      
      $('.carousel-item').first().addClass('active');
      $('#flickrPic').carousel();
      $("#flickrPic").append(newSlide);
    }
  }
 });

function makeFlickrCarousel(photoInfo) {
 var flickPicUrl = "https://farm" + photoInfo.farm +".staticflickr.com/";
 flickPicUrl += photoInfo.server + "/" + photoInfo.id + "_" + photoInfo.secret;
 flickPicUrl += "_q.jpg";

 //Build carousel pieces
 var newItem = $("<a>").addClass("carousel-item");
 var flickrImg = $("<img>").attr("src", flickPicUrl);

 newItem.append(flickrImg);

 return newItem;
}

Thanks for the help! 谢谢您的帮助!

You should remove the .initialized class from the carousel div container and move the $('#flickrPic').carousel() initialization after the for loop like this: 你应该从carousel div容器中删除.initialized类,并在for循环之后移动$('#flickrPic').carousel()初始化,如下所示:

   ...

    for(i=0; i<flickrPetPics.length; i++) {
      var newSlide = makeFlickrCarousel(flickrPetPics[i]);      
      $('.carousel-item').first().addClass('active');
      $("#flickrPic").append(newSlide);
    }
    $('#flickrPic').carousel(); // <-- Initialize the carousel after the loop has created the carousel markup

    ...

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

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