I have the following HTML code:
$(document).ready(function() {
calculate_width();
$('#moveleft').click(function() {
var loga = $('#marki #loga');
if ($('#marki .container').width() + Math.abs(parseInt(loga.css('margin-left'))) < loga.width()) {
loga.animate({
'margin-left': "-=100px"
});
}
});
$('#moveright').click(function() {
var loga = $('#marki #loga');
if (parseInt(loga.css('margin-left')) < 0) {
loga.animate({
'margin-left': "+=100px"
});
}
});
});
function calculate_width() {
var szerokosc = 0;
$('#loga a img').each(function() {
szerokosc += $(this).outerWidth(true);
});
$('#marki #loga').width(szerokosc);
}
#marki .container {
height: 50px;
overflow: hidden;
white-space: nowrap;
position: relative;
}
#marki #loga {
font-size: 0px;
}
#marki a {
display: inline-block;
margin: 0 30px 0 0;
line-height: 50px;
text-decoration: none;
}
#marki a:last-of-type {
margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="marki">
<div class="container">
<div id="loga">
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
<a href="">
<img src="http://via.placeholder.com/200x200">
</a>
</div>
</div>
</div>
<div style="padding:20px;">
<button id="moveright" style="float:left;"><<</button>
<button id="moveleft" style="float:right;">>></button>
</div>
As you can see, when I click >> button $('#marki #loga') gets 'margin-left' property to -+ 100px.
But I want to achieve, that I can scroll until the last image. But the calculation:
$('#marki .container').width() + Math.abs(parseInt(loga.css('margin-left'))) < loga.width()
is false when there are three images left to show.
What I'm calculating wrong? The list of images is getting dynamically, so I can't use the const width of images.