Skip to content

Commit

Permalink
Toggle smaller when loaded in hidden div
Browse files Browse the repository at this point in the history
  • Loading branch information
landy2005 committed Mar 7, 2019
1 parent 63b042c commit 46ca860
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions js/bootstrap-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@

var width = this.options.width || Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth())+($toggleHandle.outerWidth()/2)
var height = this.options.height || Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight())

// Outerwidth/height fix for hidden elements
if (width == 0 || height == 0) {
var toggleOnSize = getOuterSize($toggleOn);
var toggleOffSize = getOuterSize($toggleOff);
var toggleHandleSize = getOuterSize($toggleHandle);
width = this.options.width || Math.max(toggleOnSize.width, toggleOffSize.width) + (toggleHandleSize.width / 2);
height = this.options.height || Math.max(toggleOnSize.height, toggleOffSize.height) + (toggleHandleSize.height / 2);
}

$toggleOn.addClass('toggle-on')
$toggleOff.addClass('toggle-off')
this.$toggle.css({ width: width, height: height })
Expand All @@ -86,6 +96,26 @@
this.update(true)
this.trigger(true)
}

function getOuterSize(obj) {
if ($(obj).length == 0) {
return false;
}

var clone = obj.clone();
clone.css({
visibility: 'hidden',
width: '',
height: '',
maxWidth: '',
maxHeight: ''
});
$('body').append(clone);
var width = clone.outerWidth();
var height = clone.outerHeight();
clone.remove();
return {width: width, height: height};
}

Toggle.prototype.toggle = function () {
if (this.$element.prop('checked')) this.off()
Expand Down
2 changes: 1 addition & 1 deletion js/bootstrap-toggle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/bootstrap-toggle.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46ca860

Please sign in to comment.