Reputation: 6601
I have used the code in the following example:
http://jsfiddle.net/desandro/zhbLL/3/embedded/result,js,css,html,resources/
but would like to make the selected item the first item so its in the top left corner like the example below:
http://jsfiddle.net/desandro/8QkEw/
I have merged the JQuery functions from both examples, however, I am struggling getting this to work as am not great with JQuery. Can anyone help?
<div id="iso-container" class="photos clearfix">
<div class="photo">
<a href="People_JJ.jpg"><img src="People_JJ-150x150.jpg" class="small-image" alt="People_JJ" /></a>
</div>
<div class="photo">
<a href="People_CF.jpg"><img src="People_CF-150x150.jpg" class="small-image" alt="People_CF" /></a>
</div>
<div class="photo">
<a href="People_HH.jpg"><img src="People_HH-150x150.jpg" class="small-image" alt="People_HH" /></a></p>
</div>
<div class="photo">
<p><a href="People_SF.jpg"><img src="People_SF-150x150.jpg" class="small-image" alt="People_SF" /></a></p>
</div>
</div>
jQuery(function(){
var $container = jQuery('#iso-container'),
$photos = $container.find('.photo'),
$loadingIndicator = jQuery('<div class="loading"><span><img src="http://i.imgur.com/IE7iw.gif" /></span></div>');
// trigger Isotope after images have loaded
$container.imagesLoaded( function(){
$container.isotope({
itemSelector : '.photo',
masonry: {
columnWidth: 187
},
getSortData : { //code added from second example
selected : function( $item ){
// sort by selected first, then by original order
return ($item.hasClass('selected') ? -1000 : 0 ) + $item.index();
}
},
sortBy : 'selected'
});
});
// shows the large version of the image
// shows small version of previously large image
function enlargeImage( $photo ) {
$photos.filter('.large').removeClass('large');
$photos.find('.details').hide();
$photo.addClass('large');
$container.isotope('reLayout');
$photo.children('.details').show();
}
$photos.find('a').click( function() {
var $this = jQuery(this),
$photo = $this.parents('.photo');
var $previousSelected = $('.selected'); //code added from second example
if ( !$this.hasClass('selected') ) {
$this.addClass('selected');
}
$previousSelected.removeClass('selected'); //code added from second example
// update sortData for new items size //code added from second example
$container
.isotope( 'updateSortData', $this )
.isotope( 'updateSortData', $previousSelected )
.isotope();
if ( $photo.hasClass('large') ) {
// already large, just remove
$photo.removeClass('large');
$container.isotope('reLayout');
} else {
if ( $photo.hasClass('has-big-image') ) {
enlargeImage( $photo );
} else {
// add a loading indicator
$this.append( $loadingIndicator );
// create big image
var $bigImage = jQuery('<img>', { src: this.href });
// give it a wrapper and appended it to element
jQuery('<div>', { 'class': 'big-image' })
.append( $bigImage )
.appendTo( $this )
.imagesLoaded( function() {
$loadingIndicator.remove()
enlargeImage( $photo );
});
// add a class, so we'll know not to do this next time
$photo.addClass('has-big-image');
}
}
return false;
});
});
P.S.: The above worked in that it would move and open up when selected until I added the code from the 2nd example. ie the getSortData and the code from var $previousSelected = $('.selected');
Upvotes: 2
Views: 914
Reputation:
I have made some changes to your second fiddle. Is this http://jsfiddle.net/8QkEw/476/ the expected solution? HTML
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4131/5013039885_0d16ac87bc.jpg" />
</div>
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4086/5013039583_26717f6e89.jpg" />
</div>
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4144/5013039541_17f2579e33.jpg" />
</div>
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4146/5013646070_f1f44b1939.jpg" />
</div>
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4153/5013039741_d860fb640b.jpg" />
</div>
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4113/5013039697_a15e41fcd8_b.jpg" />
</div>
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4124/5013646314_c7eaf84918.jpg" />
</div>
<div class="photo">
<img class="small-image" src="http://farm5.static.flickr.com/4089/5013040075_bac12ff74e_b.jpg" />
</div>
</div>
Javascript
$(function(){
var $container = $('#container'),
$items = $('.photo');
$container.isotope({
itemSelector: '.photo',
masonry: {
columnWidth: 60
},
getSortData : {
selected : function( $item ){
// sort by selected first, then by original order
return ($item.hasClass('selected') ? -1000 : 0 ) + $item.index();
}
},
sortBy : 'selected'
})
$items.click(function(){
var $this = $(this);
// don't proceed if already selected
var $previousSelected = $('.selected');
if ( !$this.hasClass('selected') ) {
$this.addClass('selected');
}
$previousSelected.removeClass('selected');
// update sortData for new items size
$container
.isotope( 'updateSortData', $this )
.isotope( 'updateSortData', $previousSelected )
.isotope();
});
});
CSS
.photo {
width: 50px;
height: 50px;
padding:15px 5px;
float: left;
}
.photo img {
max-width:100%;
}
.photo.selected {
width: 170px;
height: auto;
background: none;
}
/* Start: Recommended Isotope styles */
/**** Isotope Filtering ****/
.isotope-item {
z-index: 2;
}
.isotope-hidden.isotope-item {
pointer-events: none;
z-index: 1;
}
/**** Isotope CSS3 transitions ****/
.isotope,
.isotope .isotope-item {
-webkit-transition-duration: 0.8s;
-moz-transition-duration: 0.8s;
transition-duration: 0.8s;
}
.isotope {
-webkit-transition-property: height, width;
-moz-transition-property: height, width;
transition-property: height, width;
}
.isotope .isotope-item {
-webkit-transition-property: -webkit-transform, opacity;
-moz-transition-property: -moz-transform, opacity;
transition-property: transform, opacity;
}
/**** disabling Isotope CSS3 transitions ****/
.isotope.no-transition,
.isotope.no-transition .isotope-item,
.isotope .isotope-item.no-transition {
-webkit-transition-duration: 0s;
-moz-transition-duration: 0s;
transition-duration: 0s;
}
/* End: Recommended Isotope styles */
Upvotes: 0
Reputation: 1193
First this code is missing in your code:
...,
getSortData : {
selected : function( $item ){
// sort by selected first, then by original order
return ($item.hasClass('selected') ? -1000 : 0 ) + $item.index();
}
},
sortBy : 'selected'
...
and
$container
.isotope( 'updateSortData', $this.parent() )
.isotope( 'updateSortData', $previousSelected )
.isotope();
return false;
});
and
var $previousSelected = $('.selected');
if ( !$this.parent().hasClass('selected') ) {
$this.parent().addClass('selected');
}
$previousSelected.removeClass('selected');
and
you have to change/or remove the width of your conainer because it is not correct
#container {
border: 2px solid;
width: 800px;
}
this is a demo
Upvotes: 1