Skip to content

Commit 1821d66

Browse files
committed
Fix dimensions issues, make style reset on destroy an option
1 parent e70c9f5 commit 1821d66

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-carousel",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"main": ["./jquery.carousel.js"],
55
"author": "Thomas Jaggi",
66
"dependencies": {

jquery.carousel.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
}
4343
},
4444
visibleSlides: 1, // how many slides to fit within visible area (0: calculate based on initial width)
45-
$syncedCarousels: null // jQuery collection of carousel elements to sync with
45+
$syncedCarousels: null, // jQuery collection of carousel elements to sync with
46+
resetInitialStylesOnDestroy: false // you get the idea
4647
},
4748

4849
utils = {
@@ -185,15 +186,18 @@
185186
this.settings = $.extend(true, {}, defaults, this.settings);
186187

187188
// Save initial styles to data attribute
188-
sliderStyles = this._getStyles($dom.slider);
189+
if (this.settings.resetInitialStylesOnDestroy) {
190+
sliderStyles = this._getStyles($dom.slider);
189191

190-
$dom.slider.data(namespace + '-css', sliderStyles);
191-
$dom.slides.each(function(){
192-
var $this = $(this),
193-
styles = self._getStyles($this);
192+
$dom.slider.data(namespace + '-css', sliderStyles);
194193

195-
$this.data(namespace + '-css', styles);
196-
});
194+
$dom.slides.each(function(){
195+
var $this = $(this),
196+
styles = self._getStyles($this);
197+
198+
$this.data(namespace + '-css', styles);
199+
});
200+
}
197201

198202
// Wrap slider with containers
199203
$dom.container.insertBefore($dom.slider);
@@ -287,12 +291,12 @@
287291
resize: function(){
288292
var containerWidth = this.$dom.frame.width(),
289293
containerHeight,
290-
slidesWidth = this.settings.behavior.horizontal ? containerWidth / this.props.visible : containerWidth,
294+
slidesWidth = this.settings.behavior.horizontal ? Math.floor(containerWidth / this.props.visible) : containerWidth,
291295
slidesHeight,
292296
sliderWidth = this.settings.behavior.horizontal ? this.props.total * slidesWidth : slidesWidth;
293297

294298
// Set new dimensions of items and slider
295-
this.$dom.slides.width(slidesWidth);
299+
this.$dom.slides.outerWidth(slidesWidth);
296300
this.$dom.slider.width(sliderWidth).height('auto');
297301

298302
if (this.settings.behavior.fixedHeight) {
@@ -526,25 +530,36 @@
526530

527531
destroy: function(){
528532
var $dom = this.$dom,
529-
sliderStyles = $dom.slider.data(namespace + '-css');
533+
sliderStyles;
530534

531535
$dom.slider
532536
.removeClass(namespace + '-slider')
533537
.removeData(namespace)
534-
.removeData(namespace + '-css')
535-
.insertAfter($dom.container)
536-
.css(sliderStyles);
538+
.insertAfter($dom.container);
537539

538540
$dom.slides
539541
.removeClass(namespace + '-slide')
540-
.removeData(namespace + '-index')
541-
.each(function(){
542+
.removeData(namespace + '-index');
543+
544+
if (this.settings.resetInitialStylesOnDestroy) {
545+
sliderStyles = $dom.slider.data(namespace + '-css');
546+
547+
$dom.slider
548+
.css(sliderStyles)
549+
.removeData(namespace + '-css');
550+
551+
$dom.slides.each(function(){
542552
var $this = $(this),
543553
styles = $this.data(namespace + '-css');
544554

545-
$this.css(styles);
546-
})
547-
.removeData(namespace + '-css');
555+
$this
556+
.css(styles)
557+
.removeData(namespace + '-css');
558+
});
559+
} else {
560+
$dom.slider.removeAttr('style');
561+
$dom.slides.removeAttr('style');
562+
}
548563

549564
$dom.container.remove();
550565

@@ -674,7 +689,7 @@
674689
}
675690

676691
$slides.each(function(){
677-
var slideHeight = $(this).css('min-height', 0).height();
692+
var slideHeight = $(this).css('min-height', 0).outerHeight();
678693

679694
if (slideHeight > height) {
680695
height = slideHeight;
@@ -735,7 +750,7 @@
735750

736751
// Return slide position
737752
_getTargetPosition: function(index){
738-
var slidesSize = this.settings.behavior.horizontal ? this.$dom.slides.width() : this.$dom.slides.height(),
753+
var slidesSize = this.settings.behavior.horizontal ? this.$dom.slides.outerWidth() : this.$dom.slides.outerHeight(),
739754
prop = this.settings.behavior.horizontal ? 'left' : 'top',
740755
css = {};
741756

@@ -785,7 +800,7 @@
785800
containerSize = this.settings.behavior.horizontal ? this.$dom.frame.width() : this.$dom.frame.height();
786801

787802
this.$dom.slides.each(function(){
788-
var size = self.settings.behavior.horizontal ? $(this).width() : $(this).height();
803+
var size = self.settings.behavior.horizontal ? $(this).outerWidth() : $(this).outerHeight();
789804

790805
if (size > minSize) {
791806
minSize = size;
@@ -803,7 +818,7 @@
803818

804819
if ($slides.length > 0) {
805820
var axis = this.settings.behavior.horizontal ? 'left' : 'top',
806-
slideDimension = this.settings.behavior.horizontal ? this.$dom.slides.width() : this.$dom.slides.height(),
821+
slideDimension = this.settings.behavior.horizontal ? this.$dom.slides.outerWidth() : this.$dom.slides.outerHeight(),
807822
currentPosition = this.$dom.slider.position(),
808823
newPosition = {};
809824

@@ -921,7 +936,7 @@
921936

922937
// Check if either swipe distance or speed was sufficient
923938
if (Math.abs(distance.x) > thresholds.distance || speed > thresholds.speed) {
924-
var refDimension = self.settings.behavior.horizontal ? self.$dom.slides.width() : self.$dom.slides.height(),
939+
var refDimension = self.settings.behavior.horizontal ? self.$dom.slides.outerWidth() : self.$dom.slides.outerHeight(),
925940
swipeDir = (distance.x > 0) ? -1 : 1,
926941
slidesSwiped = Math.abs(Math.round(distance.x / refDimension));
927942

@@ -1024,6 +1039,8 @@
10241039
this.$dom.frame.animate({
10251040
height: height
10261041
}, this.settings.animation.duration);
1042+
1043+
this.$dom.slides.css('min-height', height);
10271044
}
10281045
};
10291046

0 commit comments

Comments
 (0)