Skip to content
Browse files

delay image loading for carousel

  • Loading branch information...
1 parent 52636ec commit 8d1ad959d6ca2b7252f74f6e1e9ad850d71ce41a @game7 committed
Showing with 13 additions and 6 deletions.
  1. +1 −1 app/models/blocks/carousel.rb
  2. +12 −5 app/views/blocks/carousels/_block.html.haml
View
2 app/models/blocks/carousel.rb
@@ -10,7 +10,7 @@ def self.actions
field :shuffle, :type => Boolean, :default => false
def posts
- Post.tagged_with_any(self.tags)
+ Post.tagged_with_any(self.tags).where(:image.exists => true).newest_first.limit(self.post_count)
end
end
View
17 app/views/blocks/carousels/_block.html.haml
@@ -1,17 +1,24 @@
.carousel.slide{ :id => "carousel_#{block.id}" }
.carousel-inner
- count = 0
- - posts = block.posts.newest_first.entries
+ - posts = block.posts.entries
- posts.shuffle! if block.shuffle
- posts.each_with_index do |post, index|
- - break if count >= block.post_count
- - next unless post.image.present?
- - count += 1
%div{ :class => "item#{' active' if index == 0}" }
- = image_tag post.image.url
+ = image_tag post.image.url if index == 0
+ - unless index == 0
+ %img{ :"data-src" => post.image.url, :alt => image_alt(post.image.url) }
- unless post.title.blank?
.carousel-caption
%h4= post.title
%p= post.summary
%a.left.carousel-control{ :href => "#carousel_#{block.id}", :"data-slide" => "prev" }= "‹"
%a.right.carousel-control{ :href => "#carousel_#{block.id}", :"data-slide" => "next" }= "›"
+
+- content_for :javascript do
+ :javascript
+ $(function(){
+ $("#carousel_#{block.id} img[data-src]").each(function(index){
+ $(this).attr("src", $(this).attr("data-src"));
+ });
+ });

0 comments on commit 8d1ad95

Please sign in to comment.
Something went wrong with that request. Please try again.