-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong owl-stage width calculation when using "autoWidth: true" #1139
Comments
I had the same problem too with older versions of android. The only way I could support it in older versions of anrdroid was to use the older version of carousel or remove autoWidth in owl carousel 2. |
Any fix for this issue yet? Still bugging me. |
I've fixed it using a simple workaround: I call a function on the owl events "onInitialized", "onRefreshed". The function simply recalculates the .owl-stage width:
that's all :-) daniele |
why is this closed? |
This issue should be fixed in the version 2.1.6. If you experience any unwanted behaviour, feel free to report it via an issue. |
Version 2.1.6 fixed this issue for me. Thanks |
Actually it is still weird. The last item is still going in a new line some times. But even when the last item goes on the right place i can't move to last item |
I think I fixed it loading the carousel on window.load, instead of document ready and adding a last fake item on html |
thanks i fix this problem adding this lines of code
|
precendent fix don't work for me, i have to add 1 to this code
The problem i think is that coordinates function calculate ceil on negative value (es -37.5 --> -37) Css width for owl-stage is taken by last coordinates that is 1px short (es. 37 and not 38) |
I can confirm that this is still a problem in version 2.2.1 (using autoWidth=true). If I change width on owl-stage just by adding 1px then the issue is resolved. Another solution that worked for me is setting box display on owl-stage: Best regards, |
@masbug Thanks alot! It works perfectly fine now. |
what worked for me was adding a rule for |
@masbug thanks!!! works well for me.. spent a lot of time finding workaround and finally it's fxed )) |
@masbug It should be display: flex;, but it still doesn't work for me every time. I have to reload. Before I do, all the items are scrunched up. |
Also @masbug that does seem to work, but not with center: true, as it throws the alignment of the slides off. |
I've encountered this issue this morning // Fix for last item creating a new line with autoWidth=true
var $this = $('.owl-carousel').owlCarousel({ autoWidth: true });
var $stageOuter = $this.find('.owl-stage-outer');
var stageOuterWidth = $stageOuter.width();
var $stage = $this.find('.owl-stage');
var stageWidth = $stage.width();
var $items = $stage.children();
var largestChild = 0;
for (var i = 0, ic = $items.length; i < ic; i++) {
var itemWidth = $($items[i]).width();
largestChild = largestChild > itemWidth ? largestChild : itemWidth;
}
if (largestChild > stageOuterWidth) {
$stage.width(stageWidth + Math.ceil((largestChild - stageOuterWidth) * 1.2));
var addedWidth = Math.ceil((largestChild - stageOuterWidth) * 1.2)
var stageNewWidth = $stage.width();
console.log(stageOuterWidth, stageWidth, stageNewWidth, addedWidth, largestChild);
} The 1.2 factor is there to assure the width isn't still short, it needs to be 1 or higher, putting 0.9 will make the second line appear again The solution I applied is triggering a refresh on the carousel right after initialisation, resizing the window recalculates the stage width correctly var $this = $('.owl-carousel').owlCarousel({ autoWidth: true }).trigger('refresh.owl.carousel'); I tried multiple events: Edit: tested with current release |
@adigourdi - Thanks. Triggering a refresh after initialization worked for me as well. |
Hi. I had the same problem. The script didn't understand stage width, because images haven't been uploaded. To solve this problem I used jQuery plugin "Images Loaded" ( https://github.com/desandro/imagesloaded ) var carousel = $( '.owl-carousel' ).owlCarousel({
margin: 25,
loop: true,
dots: false,
autoWidth: true,
items: 3
});
carousel.imagesLoaded( function() {
carousel.trigger( 'refresh.owl.carousel' );
}); I hope this helps someone) |
@adigourdi - Thanks this worked for me as well. |
A simple solution. Double the preset .owl-stage width:
|
I got same problem today,
|
Same issue solved by 'onInitialized' and 'onRefreshed' my code is below |
I ran into this same problem and I think it's related to #604. It's not strictly limited to google fonts--any loaded font causes this problem. What happens is that it calculates the owl-stage based on a generic font and then once your font is loaded it causes the stage to be too small (because whatever font you are using causes your element to be a little bigger). A true fix would recalculate the stage whenever there's a change in the item size. I think just doubling the stage width size does the trick, but one could also use something like this which can trigger once a font is loaded and then trigger a refresh on the owl. |
For me, the only solution that worked was the one with adding CSS-styles for box-styles as provided by @masbug – triggering a "refresh.owl.carousel" or something with "onInitialized" / "onRefreshed" didn't work. |
Hi all, it doesn't work well for neither solutions |
@choogoor the fix by kampiamodikuesto (the 1 + Math.ceil in owl.carousel.js ) fix worked nice for me |
None of these solutions worked for me. The only thing that worked for me is adding
|
all the above solutions did not work for me, so I did it myself by using CSS this is what i did... .owl-carousel .owl-stage { .owl-item { .item { .owl-carousel .owl-item img { This works for me, hope it helps someone too. |
More Thanks, If not work, Please try !important |
@masbug solution worked for me. thanks.
|
The source of the problem is that line: widths[iterator] = !grid ? this._items[iterator].width() : width * merge; in Chrome you often get width as 0. I think that author tried to resolve such issues by using an autoRefresh option. However it's not reliable as you do not know how long does it take to load all images. Solutions with initialized handler may not work, because there may be not enough time to correctly load and recalculate width of all images by browser. Solution with imagesLoaded is better, but still not perfect. Firefox has no problems with it, but Chrome does. Sometimes latest elements with images may still return zero width, because browser wasn't able to calculate new width of parent elements even if images were loaded. This is why I created my own solution. function isOwlLoaded(carousel) {
let iterator = carousel._items.length;
while (iterator--) {
if (!carousel._items[iterator].width()) {
setTimeout(isOwlLoaded.bind(null, carousel), 100);
return false;
}
}
carousel.refresh();
}
$(".owl").owlCarousel({
autoWidth: true,
onInitialized: function () {
isOwlLoaded(this);
},
}); It checks if all items return non-zero width and re-run the check if not. If that condition is met, it refreshes the owl object. It may need some extra work if you use some lazy-loaded items, which should be ignored and so on. There also should be some limit to prevent infinite loop if image wasn't loaded. |
I had the same problem, and fixed it by :
|
Why is this closed? This is still happening in 2.3.4 |
Unfortunately, I can confirm that on version 2.3.4 bug still occur. |
Solved by adding this code:
load event fires after all images are loaded. |
I am experiencing the same issue, and it persists even after I tried turning "autoWidth" back to false. |
Hello,
I have a strange bug... I have several pages with a carousel with same option
On same of this pages the carousel doesn't work correctly: the width of the owl-stage element is calculated wrongly and the last item of the carousel goes on a new line (under the firs item in chrome ora under the last item of the first row in firefox). for example look at this page:
http://www.galleriacarlasozzani.org/en/past?milano/2010
the main issue is that it is random... on the vast majority of page everything work fine and the pages with the bug are have exactly the same layout and template of the other one.
Do you have any idea about fixing this?
thanks
daniele
The text was updated successfully, but these errors were encountered: