Skip to content

Instantly share code, notes, and snippets.

@solepixel
Last active June 15, 2022 02:24
Show Gist options
  • Save solepixel/9b555388d56a32ee2ec7 to your computer and use it in GitHub Desktop.
Save solepixel/9b555388d56a32ee2ec7 to your computer and use it in GitHub Desktop.
Vertically and horizontally center Soliloquy slides while still keeping proportions. Emulating the "background-size: cover" with a standard slider. Slider configuration looks like this: Slider Dimensions: Full Width, Slider Position: Center, Use Adaptive Height: Yes, Crop Images in Slider: Yes
(function( $ ){
'use strict';
function auto_fit_soliloquy( parent_selector ){
// resize the slider container
var $soliloquy = $( parent_selector + ' .soliloquy-container'),
$all_items = $('.soliloquy-slider,.soliloquy-viewport,.soliloquy-item', $soliloquy ),
$container = $('.soliloquy-item', $soliloquy ),
window_height = $(window).height();
$all_items.css('height', window_height ).css('overflow','hidden');
// set image size
var th = $container.height(), // box height
tw = $container.width(), // box width
$im = $('.soliloquy-image', $container ), // image
ih = $im.height(), // inital image height
iw = $im.width(); // initial image width
if ( ih > iw ) { // if portrait
$im.addClass('ww').removeClass('wh'); // set width 100%
} else { // if landscape
$im.addClass('wh').removeClass('ww'); // set height 100%
}
// set image offset
var nh = $im.height(), // new image height
nw = $im.width(), // new image width
hd = ( nh - th ) / 2, // half dif img/box height
wd = ( nw - tw ) / 2; // half dif img/box width
if ( nh < nw ) { // if portrait
$im.css({
'margin-left': '-' + wd + 'px',
'margin-top': 0,
'max-width': 'none'
}); // offset left
} else { // if landscape
$im.css({
'margin-top': '-' + hd + 'px',
'margin-left': 0,
'max-width': 'none'
}); // offset top
}
}
// document ready event
$(function(){
auto_fit_soliloquy( '.selector-for-your-slider-container-parent' );
});
// window resize event
$(window).on('resize', function(){
auto_fit_soliloquy( '.selector-for-your-slider-container-parent' );
});
})( jQuery );
.soliloquy-container .soliloquy-image {
min-width:100%;
}
.soliloquy-container .soliloquy-image.wh{
min-height:100% !important;
}
.soliloquy-container .soliloquy-image.ww{
width:100% !important;
}
@jdh1977
Copy link

jdh1977 commented Jun 8, 2016

This works great! How would you go about offsetting a class/id? So for example if you had a .site-header that was 90px high?

Thanks

@jdh1977
Copy link

jdh1977 commented Jun 8, 2016

Answered my own question..

// resize the slider container var $soliloquy = $( parent_selector + ' .soliloquy-container'), $all_items = $('.soliloquy-slider,.soliloquy-viewport,.soliloquy-item', $soliloquy ), $container = $('.soliloquy-item', $soliloquy ), window_height = $(window).height() **- $('.site-header').outerHeight();**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment