<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>慕課七夕主題</title>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
ul,
li {
list-style-type: none;
}
#content {
width: 100%;
height: 100%;
/* top: 20%;
left: 20%; */
overflow: hidden;
position: absolute;
}
.content-wrap {
position: relative;
}
.content-wrap > li {
background: #CAE1FF;
color: red;
float: left;
overflow: hidden;
position: relative;
}
li:nth-child(2) {
background: #9BCD9B;
}
li:nth-child(3) {
background: yellow;
}
button {
width: 100px;
height: 50px;
}
.button {
position: absolute;
bottom: 0;
}
</style>
</head>
<body>
<div id='content'>
<ul class='content-wrap'>
<!-- 第一副畫面 -->
<li> 頁面1 </li>
<!-- 第二副畫面 -->
<li> 頁面2 </li>
<!-- 第三副畫面 -->
<li> 頁面3 </li>
</ul>
<div class="button">
<button>點擊切換頁面</button>
</div>
</div>
<script type="text/javascript">
var container = $("#content");
// 獲取第一個子節點
var element = container.find(":first");
// li頁面數量
var slides = element.find("li");
// 獲取容器尺寸
var width = container.width();
var height = container.height();
// 設置li頁面總寬度
element.css({
width: (slides.length * width) + 'px',
height: height + 'px'
});
// 設置每一個頁面li的寬度
$.each(slides, function(index) {
var slide = slides.eq(index); // 獲取到每一個li元素
slide.css({ // 設置每一個li的尺寸
width: width + 'px',
height: height + 'px'
});
});
// 綁定一個事件,觸發通過
$('button').click(function() {
// 在5秒的時間內,移動X的位置,為2個頁面單位
//?
element.css({
'transition-timing-function': 'linear',
'transition-duration': '10000ms',
'transform': 'translate3d(-' + (width * 2) + 'px,0px,0px)' //設置頁面X軸移動
});
});
</script>
</body>
</html>
在頁面子元素單一的情況下通過設置父元素的定位實現頁面切換會比設置子元素簡單
transform屬性是靜態屬性,不是動畫屬性,一旦寫到style里面,將會直接顯示作用,無任何變化過程
通過設置transition的一些參數,讓translate3d這個屬性發生變化
