为什么这个带有“vertical-align:bottom”的div的子元素在淡出之前移动到页面的中间?

时间:2015-11-16 19:11:43

标签: javascript jquery html css css3

JSFiddle here

$(document).ready(function() {
	//alert("slider.js detected");//check

	var numberOfImages = $('#imageSlideshowContainer > img').length;
	var currentImage = 1;

	/**
	* Previous Arrow Code
	**/
	$('.first-viewport .previous-slide-arrow').click(function () {
		$('img.slider-image' + currentImage).hide();
		$('span.caption' + currentImage).hide();
		$('.central-content-container .navigation-bullets-wrapper a.navigation-bullet' + currentImage).removeClass('active');

		currentImage--;

		if (currentImage == 0) {
			currentImage = numberOfImages;
		}
		
		$('img.slider-image' + currentImage).show();
		$('span.caption' + currentImage).show();
		$('.central-content-container .navigation-bullets-wrapper a.navigation-bullet' + currentImage).addClass('active');
		
		return false;
	});


	/**
	* Next Arrow Code
	**/
	$('.first-viewport .next-slide-arrow').click(function () {
		$('img.slider-image' + currentImage).hide();
		$('span.caption' + currentImage).hide();
		$('.central-content-container .navigation-bullets-wrapper a.navigation-bullet' + currentImage).removeClass('active');

		currentImage++;

		if (currentImage == numberOfImages + 1) {
			currentImage = 1;
		}
		
		$('img.slider-image' + currentImage).show();
		$('span.caption' + currentImage).show();
		$('.central-content-container .navigation-bullets-wrapper a.navigation-bullet' + currentImage).addClass('active');
		
		return false;
	});

	/**
	* Bullets Code
	**/
	function changeImage(imageNumber) {
		$('img.slider-image' + currentImage).hide();
		$('span.caption' + currentImage).hide();
		currentImage = imageNumber;
		$('img.slider-image' + currentImage).show();
		$('span.caption' + currentImage).show();
		
		$('.central-content-container .navigation-bullets-wrapper a').removeClass('active');
		
		$('.central-content-container .navigation-bullets-wrapper a.navigation-bullet' + imageNumber).addClass('active');
	}


	/**
	* Automatic Timer to change Slides
	* Copy paste the same code from next arrow, and insert a special function setInterval which will run another function every x seconds you set.
	**/
	function autoChangeSlides() {
		$('img.slider-image' + currentImage).fadeOut(800);
		$('span.caption' + currentImage).fadeOut(800);
		$('.central-content-container .navigation-bullets-wrapper a.navigation-bullet' + currentImage).removeClass('active');

		currentImage++;

		if (currentImage == numberOfImages + 1) {
			currentImage = 1;
		}
		
		$('img.slider-image' + currentImage).fadeIn(800);
		$('span.caption' + currentImage).fadeIn(800);
		$('.central-content-container .navigation-bullets-wrapper a.navigation-bullet' + currentImage).addClass('active');
	}

	var slideTimer = setInterval( function() { autoChangeSlides() }, 2000);

});
html, body {
	height:100%;
}

.first-page {
	height:100%;
}

#imageSlideshowContainer img {
	top:0px;
	left:0px;
	position:fixed;
	width:100%;
	display:none;
	
	height:100%; /*If I don't set this, the image just remains small, confined within the first viewport in the browser, it doesn't for example peep through the translucence of the footer and other sections.*/
}

#imageSlideshowContainer img.slider-image1 {
	display:block;
}

.captionSlideshowContainer { /*same as first-viewport*/
	width:100%;
	display:table;
	/*top:0px;*/
	
	height:100%;
}

.previous-slide-arrow, .next-slide-arrow {
	display:table-cell;
	vertical-align:middle;
	position:relative;/*wat if not?*/
	color:white;
	text-align:left;
	text-decoration:none;
	background-repeat:no-repeat;
	background-position:center center;
	left:20px;
	width:3%;
	
	font-size:50px;
	font-weight:bold;
}

.next-slide-arrow {
	left:auto;
	right:20px;
	text-align:right;
}

.central-content-container {
	display:table-cell;
	text-align:center;
	vertical-align:bottom;
	position:relative;
	bottom:30px;
	padding:20px 5px;
}

.slider-text-wrapper span {
	display:none;
}

.caption1 {
	display:block;
}

.slider-text-wrapper span h3 {
	text-transform:uppercase;
	font-size:68px;
	font-weight:bold;
	font-family: sans-serif;
	color:orange;
	line-height:60px;
}

.slider-text-wrapper span p {
	font-size:20px;
	font-weight:100;
	font-family: sans-serif;
	color:white;
	line-height:60px;
}

.central-content-container a {
	width:6px;
	height:6px;
	display:inline-block;
	margin-right:16px;
	background: white none repeat scroll 0% 0%;
	border-radius:100%;
	color:transparent;
}

.central-content-container a.active {
	background:yellow;
}


















/*
height of 100% not same as that of document
removing relative position removes caption text



*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first-page">
		<div id="imageSlideshowContainer" class="image-slideshow-container">
			<img class="slider-image1" src="http://www.stadiumsofprofootball.com/nfc/images/candold2.jpg" alt="pixer!" /> 
			<img class="slider-image2" src="http://www.bryantpark.org/static/galleries/history/18.jpg" alt="pixer!" /> 
			<img class="slider-image3" src="http://www.legendsofamerica.com/photos-CO-Misc/CheesmanPark.jpg" alt="pixer!" /> 
			<img class="slider-image4" src="https://upload.wikimedia.org/wikipedia/commons/2/2d/Sportsman's_Park_1946_World_Series-1.jpg" alt="pixer!" /> 
			<img class="slider-image5" src="http://www.ballparksofbaseball.com/past/images/cand13959.jpeg" alt="pixer!" /> 
			<img class="slider-image6" src="https://upload.wikimedia.org/wikipedia/commons/3/38/Central_Park_New_York_City_New_York_8.jpg" alt="pixer!" /> 
		</div>


		<div class="captionSlideshowContainer first-viewport">
			<a class="previous-slide-arrow" href="#">&lt;</a>
			
			<div class="central-content-container">
				<div class="slider-text-wrapper">
					<span class="caption1" style="display:block;" ><h3>alpha alpha alpha</h3></span>
					<span class="caption2" ><h3 >beta beta beta</h3></span>
					<span class="caption3" ><h3>Gamma gamma Gamma</h3></span>
					<span class="caption4" ><h3>Delta delta Delta</h3></span>
					<span class="caption5" ><h3>Epsilon epsilon</h3></span>
					<span class="caption6" ><h3>Neither Either Whatever</h3></span>
				</div>
				<!--<div class="learn-more-link-wrapper">
					<img class="learn-more-image" src="http://localhost/moodle/theme/crystal/pix/headings/learn-more-image.png" />
				</div>-->
				<div class="navigation-bullets-wrapper">
					<a class="active navigation-bullet1" href="javascript: changeImage(1)"><span>Bullet</span></a>
					<a class="navigation-bullet2" href="javascript: changeImage(2)"><span>Bullet</span></a>
					<a class="navigation-bullet3" href="javascript: changeImage(3)"><span>Bullet</span></a>
					<a class="navigation-bullet4" href="javascript: changeImage(4)"><span>Bullet</span></a>
					<a class="navigation-bullet5" href="javascript: changeImage(5)"><span>Bullet</span></a>
					<a class="navigation-bullet6" href="javascript: changeImage(6)"><span>Bullet</span></a>
				</div>
			</div>
			
			<a class="next-slide-arrow" href="#">&gt;</a>
		</div> <!-- .first-viewport -->
	
	</div> <!-- .first-page -->

	<p style="background-color:pink; opacity:0.5;">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32.
	
    The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    
	Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32.
	
	The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from de Finibus Bonorum et Malorum by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
	
	There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>

在此SSCCE中,.captionSlideshowContainer已应用display:table-cellvertical-align:bottom。但是在JQuery幻灯片放映期间,当现有的span.captionX 淡出时,它会在隐藏之前捕捉到页面的中间位置。

之前我使用的是show()hide(),而不是fadeIn()fadeOut(),而这种情况并没有发生。

问题是为什么会发生这种情况,我应该怎么做呢?

1 个答案:

答案 0 :(得分:2)

这是因为当元素淡出时,它仍占用页面上的空间,直到它完全褪色(display:none)并且另一个进入其位置需要考虑到没有的元素完全消失了。

当你刚刚使用show或hide时,随着时间的推移没有动画,所以它立即被隐藏起来。

您可以尝试绝对定位包含文本的元素,使其脱离文档流,然后它将无关心它相对于其他元素的位置。

它不完美,但这就是这个想法:

.slider-text-wrapper span {
    display:none;
    position: absolute;
    top: 30px;
    left: 30px;
}

小提琴:http://jsfiddle.net/vg3q91v6/2/

以下评论@showdev实际上有一个更好的例子。