jquery - Javascript vertically centering the active element -
i have carousel in website doing, need implement function center image vertically when item active, function this:
function vertically_centred(first_box_id, second_box_id){ window.onresize = function() { var height1 = document.getelementbyid(first_box_id).offsetheight; var height = document.getelementbyid(second_box_id).offsetheight; var total = (height / 2 ) - (height1 / 2); var color = document.getelementbyid(first_box_id); color.style.top = total + "px"; }; }
i have 3 items 2 box inside different ids, function works perfectly, when carousel changes next item function doesn't work have implemented function adding in html file:
<script> vertically_centred('text-1', 'item-1'); vertically_centred('text-2', 'item-2'); vertically_centred('text-3', 'item-3'); </script>
i think needs detect if item active use function , not know how it.
html:
<div id="mycarousel" class="carousel"> <ol class="carousel-indicators"> <li class="active" data-target="#mycarousel" data-slide-to="0"></li> <li data-target="#mycarousel" data-slide-to="1"></li> <li data-target="#mycarousel" data-slide-to="2"></li> </ol> <div class="carousel-inner"> <div id="item-1" class="active item"> <div class="container"> <div id='text-1' class="text-center-resizable"> <img src="images/welcome_img.png" alt="cover"> welcome test1 </div> </div> </div> <div id="item-2" class="item"> <div class="container"> <div id="text-2" class="text-center-resizable"> <img src="images/welcome_img.png" alt="cover"> welcome test2 </div> </div> </div> <div id="item-3" class="item"> <div class="container"> <div id="text-3" class="text-center-resizable"> <img src="images/cover.jpg" alt="cover"> welcome test3 </div> </div> </div> </div> <a href="#mycarousel " class="left carousel-control" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span> </a> <a href="#mycarousel" class="right carousel-control" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span> </a> </div>
css
/* big screen min size 13px , max when screen bigger 16px*/ @media (max-width: 1200px) { .text-center-resizable { font-size: 13px; position: absolute; left: 0; right: 0; margin: auto; padding-top: 20px; padding-bottom: 20px; color: #ffffff; text-align: center; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6); } } @media (min-width: 1200px) { .text-center-resizable { font-size: 16px; position: absolute; left: 0; right: 0; margin: auto; padding-top: 20px; padding-bottom: 20px; color: #ffffff; text-align: center; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6); } } /* end of class */ #mycarousel { background-color: #222; background-attachment: fixed; background-repeat: no-repeat; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; padding: 0; }
i resolved problem changing centered element, if needs in future leave code right down:
.
text-center-resizable { color: #ffffff; text-align: center; margin: 0; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) }
Comments
Post a Comment