[英]Javascript vertically centering the active element
我正在做的网站上有一个旋转木马,当项目处于活动状态时,我需要实现一个使图像垂直居中的功能,该功能是这样的:
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";
};
}
我有3个项目,里面有2个具有不同ID的框,该函数可以正常工作,但是当轮播更改为下一个项目时,该函数不起作用,我已通过在html文件中添加此函数来实现此函数:
<script>
vertically_centred('text-1', 'item-1');
vertically_centred('text-2', 'item-2');
vertically_centred('text-3', 'item-3');
</script>
我认为它需要检测项目是否处于活动状态,然后使用该功能,但我不知道该怎么做。
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;
}
我解决了更改它以元素为中心的问题,如果将来有人需要它,我将直接保留代码:
。
text-center-resizable {
color: #ffffff;
text-align: center;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
每次调用vertical_centred时,它只是重置window.onresize函数。 因此,先前的值会丢失。 尝试这样的事情:
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');
}
然后,您的下一个和上一个按钮可以定义为:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.