我的网站上有旋转木马,我需要实现一个功能,当项目处于活动状态时垂直居中,功能如下:
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文件中添加此函数来实现此功能:< / p>
<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;
}
答案 0 :(得分:0)
我解决了将元素置于元素中心的问题,如果将来有人需要这个,我会将代码保留下来:
text-center-resizable {
color: #ffffff;
text-align: center;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
答案 1 :(得分:-2)
每次调用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>