<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>慕課七夕主題</title>
<link rel='stylesheet' href='style.css' />
<link rel='stylesheet' href='pageA.css' />
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript" src="Swipe.js"></script>
</head>
<style type="text/css">
.charector {
width: 151px;
height: 291px;
left: 6%;
top: 55%;
position: absolute;
background: url(http://img1.sycdn.imooc.com//55ade248000198ae10550582.png) -0px -291px no-repeat;
}
</style>
<body>
<div id='content'>
<ul class='content-wrap'>
<li>
<div class="a_background">
<div class="a_background_top"></div>
<div class="a_background_middle"></div>
<div class="a_background_botton"></div>
</div>
</li>
<li> 頁面2 </li>
<li> 頁面3 </li>
</ul>
<div id="boy" class="charector"></div>
</div>
<script type="text/javascript">
var swipe = Swipe($("#content"));
var $boy = $("#boy");
// 設置一下縮放比例與基點位置
var proportion = $(document).width() / 1440;
// 設置元素縮放
$boy.css({
transform: 'scale(' + proportion + ')'
});
// 獲取數據
var getValue = function(className) {
var $elem = $('' + className + '');
// 走路的路線坐標
return {
height: $elem.height(),
top: $elem.position().top
};
}
// 路的中間到頂部的距離
var pathY = function() {
var data = getValue('.a_background_middle');
return data.top + data.height / 2;
}();
// 獲取人物元素布局尺寸
var boyHeight = $boy.height();
var boyWidth = $boy.width();
// 計算下縮放后的元素與實際尺寸的一個距離
var boyInsideLeft = (boyWidth - (boyWidth*proportion))/2;
var boyInsideTop = (boyHeight - (boyHeight*proportion))/2;
// 修正小男孩的正確位置
// 中間路的垂直距離 - 人物原始的垂直距離 - 人物縮放后的垂直距離
$boy.css({
top: pathY - (boyHeight * proportion) - boyInsideTop
});
</script>
</body>
</html>
/////////
//頁面滑動 //
/////////
/**
* [Swipe description]
* @param {[type]} container [頁面容器節點]
* @param {[type]} options [參數]
*/
function Swipe(container) {
// 獲取第一個子節點
var element = container.find(":first");
var swipe = {};
// li頁面數量
var slides = element.find(">");
// 獲取容器尺寸
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({
width: width + 'px',
height: height + 'px'
});
});
// 監控完成與移動
swipe.scrollTo = function(x, speed) {
// 執行動畫移動
element.css({
'transition-timing-function' : 'linear',
'transition-duration' : speed + 'ms',
'transform' : 'translate3d(-' + x + 'px,0px,0px)'
});
return this;
};
return swipe;
}
* {
padding: 0;
margin: 0;
}
ol,
ul,
li {
list-style-type: none;
}
/*主體部分*/
#content {
width: 100%;
height: 100%;
/*top: 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;
}
a {
position: absolute;
top: 50%;
left: 40%;
}
/*背景圖*/
.a_background {
width: 100%;
height: 100%;
position: absolute;
}
.a_background_top{
width: 100%;
height: 71.6%;
background-image: url("http://img1.sycdn.imooc.com//55addf6900019d8f14410645.png");
background-size: 100% 100%;
}
.a_background_middle{
width: 100%;
height: 13.1%;
background-image: url("http://img1.sycdn.imooc.com//55addf800001ff2e14410118.png");
background-size: 100% 100%;
}
.a_background_botton{
width: 100%;
height: 15.3%;
background-image: url("http://img1.sycdn.imooc.com//55addfcb000189b314410138.png");
background-size: 100% 100%;
}