Reputation: 2711
for some reason the prependTo() function in jQuery is not working for me. It's probably something simple that I'm missing but I Can't see it at all. I'm basically just trying to get the last image to appear at the start through jQuery.
$(document).ready(function(){
var slide_count = $(".carousel li").length;
var slide_width = $(".carousel li").width();
var slide_height = $(".carousel li").height();
var cont_width = slide_width * slide_count;
$(".cont").css({ height: slide_height, width: slide_width});
$(".carousel").css({ width: cont_width, marginLeft: - slide_width });
$(".carousel li:last-child").prependTo(".carousel");
});
body{
margin: 0;
padding: 0;
}
.cont{
text-align: center;
font-size: 0;/*removes white space*/
margin: 60px auto 0 auto;
}
.carousel{
position: relative;
margin: 0;
padding: 0;
list-style-type: none;
height: 100%;
max-height: 600px;
}
.carousel li{
float: left;
width: 750px;
height: 350px;
}
.carousel li img{
width: 100%;
height: auto;
}
.next{
position: absolute;
top: 0;
right: 0;
width: 40px;
height: 40px;
background-color: blue;
}
.prev{
position: absolute;
top: 0;
left: 0;
width: 40px;
height: 40px;
background-color: blue;
}
<div class="cont">
<ul class="carousel">
<div class="prev">
</div>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-2.jpg" alt="" />
</li>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-6.jpg" alt="" />
</li>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-1.jpg" alt="" />
</li>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-3.jpg" alt="" />
</li>
<div class="next">
</div>
</ul>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
Upvotes: 1
Views: 418
Reputation: 628
Try this
$(document).ready(function(){
var slide_count = $(".carousel li").length;
var slide_width = $(".carousel li").width();
var slide_height = $(".carousel li").height();
var cont_width = slide_width * slide_count;
$(".cont").css({ height: slide_height, width: slide_width});
$(".carousel").css({ width: cont_width, marginLeft: - slide_width });
$(".carousel li").last().prependTo(".carousel");
});
body{
margin: 0;
padding: 0;
}
.cont{
text-align: center;
font-size: 0;/*removes white space*/
margin: 60px auto 0 auto;
}
.carousel{
position: relative;
margin: 0;
padding: 0;
list-style-type: none;
height: 100%;
max-height: 600px;
}
.carousel li{
float: left;
width: 750px;
height: 350px;
}
.carousel li img{
width: 100%;
height: auto;
}
.next{
position: absolute;
top: 0;
right: 0;
width: 40px;
height: 40px;
background-color: blue;
}
.prev{
position: absolute;
top: 0;
left: 0;
width: 40px;
height: 40px;
background-color: blue;
}
<div class="cont">
<ul class="carousel">
<div class="prev">
</div>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-2.jpg" alt="" />
</li>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-6.jpg" alt="" />
</li>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-1.jpg" alt="" />
</li>
<li>
<img src="http://lorempixel.com/output/abstract-q-c-1500-700-3.jpg" alt="" />
</li>
<div class="next">
</div>
</ul>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
Upvotes: 0
Reputation: 337560
The issue is because :last-child
is looking for children of the li
, not the last li
itself. You need to use :last
instead:
$(".carousel li:last").prependTo(".carousel");
Also note that your HTML is invalid as you cannot have a div
as a child of a ul
. You'll need to put them outside the ul
or inside another li
.
Upvotes: 1