简体   繁体   中英

Javascript vertically centering the active element

I have a carousel in my website I am doing, I need to implement a function that center the image vertically when item is active, the function is 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 with 2 box inside with different ids, the function works perfectly, however when carousel changes to the next item the function doesn't work i have implemented this function by adding this in my html file:

<script>
    vertically_centred('text-1', 'item-1');
    vertically_centred('text-2', 'item-2');
    vertically_centred('text-3', 'item-3');
</script>

I think it needs to detect if item is active then use the function and I do not know how to do 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

/* This is for Big Screen the min size is 13px and the max when
    screen is bigger its 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 the 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 the problem changing the it centered the element, if someone needs this in the future i will leave the code right down:

.

text-center-resizable {
        color: #ffffff;
        text-align: center;
        margin: 0;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%)
    }

Every time vertically_centred is called, it is just resetting the window.onresize function. So the previous values are lost. Try something like this instead:

function vertically_centred(first_box_id, second_box_id){
    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";
}

function centerItems(){
    vertically_centred('text-1', 'item-1');
    vertically_centred('text-2', 'item-2');
    vertically_centred('text-3', 'item-3');
}

And then your next and previous buttons could be defined as:

<a href="#myCarousel" onclick="centerItems()" class="left carousel-control" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span> </a>
<a href="#myCarousel" onclick="centerItems()" class="right carousel-control" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span> </a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM