老师,这个例子的源码在哪
老师,这个例子的源码在哪
老师,这个例子的源码在哪
2016-05-31
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js+css实现带预览图幻灯片效果</title>
<style>
*{margin:0;padding:0;}
body{padding:50px 0;font-size:14px;font-family:"Microsoft YaHei UI";background:#fff;color:#555;-webkit-font-smoothing:antialiased;}
.main-i,.main,.slider{width:100%;height:400px;position:relative;}
/*幻灯片区域*/
.slider .main{overflow:hidden;}
.main-i{opacity:0;position:absolute;right:50%;top:0;-webkit-transition:all .5s;z-index:2;}
.main-i_right{right:-50%;}
#main_background,.main-i_active{opacity:1;right:0;z-index:2;}
#main_background{z-index:1;}
.main-i img{width:100%;position:absolute;left:0;top:50%;z-index:1;}
.caption{position:absolute;right:64%;top:30%;z-index:9;}
.caption h2{font-size:40px;line-height:50px;color:#b5b5b5;text-align:right;margin-right:45px;}
.caption h3{font-size:70px;line-height:70px;color:#000;text-align:right;font-family: open-sans-condensed;margin-right:-45px;}
.caption h2,.caption h3{-webkit-transition:all 1s .8s;opacity:0;}
/*控制按钮区域*/
.slider .ctrl{width:100%;height:13px;line-height:13px;text-align:center;position:absolute;left:0;bottom:-13px;/*background-color:#f00;*/}
.ctrl-i{display:inline-block;width:150px;height:13px;background:#666;box-shadow:0 1px 1px rgba(0,0,0,.3);position:relative;margin-left:1px;}
.ctrl-i img{width:100%;position:absolute;left:0;bottom:50px;z-index:1; opacity:0;-webkit-transition:all .2s;}
/*hover*/
.ctrl-i:hover{background:#f0f0f0;}
.ctrl-i:hover img{bottom:13px;-webkit-box-reflect:below 0px -webkit-gradient(
linear,
left top,
left bottom,
from( transparent ),
color-stop( 50%,transparent ),
to( rgba(255,255,255,.3) )
);
opacity:1;
}
/*active*/
.ctrl-i_active:hover,.ctrl-i_active{background:#000;}
.ctrl-i_active:hover img{opacity:0;}
.main-i_active h2,.main-i_active h3{margin-right:0;opacity:1;}
</style>
</head>
<body>
<div class="slider">
<!--0.修改VIEW ——> Template(关键字替换),增加template id -->
<div class="main" id="template_main">
<!--幻灯片模板-->
<div class="main-i {{css}}" id="main_{{index}}">
<div class="caption">
<h2>{{h2}}</h2>
<h3>{{h3}}</h3>
</div>
<img src="images/{{index}}.jpg" class="picture" />
</div>
</div>
<div class="ctrl" id="template_ctrl">
<a href="javascript:switchSlider({{index}});" class="ctrl-i" id="ctrl_{{index}}"><img src="images/{{index}}.jpg" alt=""></a>
</div>
</div>
<script>
//1.数据定义(实际生产环境中,应有后台给出)
var data = [
{img:1,h1:'Creative',h2:'DUET'},
{img:2,h1:'Friendly',h2:'DEVIL'},
{img:3,h1:'Tranquilent',h2:'COMPATRIOT'},
{img:4,h1:'Insecure',h2:'HUSSLER'},
{img:5,h1:'Loving',h2:'REBEL'},
{img:6,h1:'Passionate',h2:'SEEKER'},
{img:7,h1:'Crazy',h2:'FRIEND'}
];
//2.通用函数
var g = function(id){
if(id.substr(0,1) == "."){
return document.getElementsByClassName(id.substr(1));
}
return document.getElementById(id);
}
//3.添加所有幻灯片&对应按钮
function addSliders(){
//3.1获取模板
var tpl_main= g('template_main').innerHTML
.replace(/^\s*/,'')
.replace(/\s*$/,'');
var tpl_ctrl= g('template_ctrl').innerHTML
.replace(/^\s*/,'')
.replace(/\s*$/,'');
//3.2定义最终输出 html的变量
var out_main = []; //所有幻灯片变量
var out_ctrl = []; //所有控制按钮变量
//3.3遍历所有数据,构建最终输出的 HTML
for(i in data){
var _html_main = tpl_main
.replace(/{{index}}/g,data[i].img)
.replace(/{{h2}}/g,data[i].h1)
.replace(/{{h3}}/g,data[i].h2)
.replace(/{{css}}/g,['','main-i_right'][i%2]);
var _html_ctrl = tpl_ctrl
.replace(/{{index}}/g,data[i].img);
out_main.push(_html_main);
out_ctrl.push(_html_ctrl);
}
//3.4吧HTML回写到对应的DOM里面
g('template_main').innerHTML = out_main.join('');
g('template_ctrl').innerHTML = out_ctrl.join('');
//7.增加 #main_background,以免出现空白背景
g('template_main').innerHTML += tpl_main
.replace(/{{index}}/g,'{{index}}');
g('main_{{index}}').id = 'main_background';
}
//5.幻灯片切换
function switchSlider(n){
//5.1获得要展现的幻丁片&控制按钮 DOM
var main = g('main_'+n);
var ctrl = g('ctrl_'+n);
//5.2获得所有幻灯片以及控制按钮
var clear_main = g('.main-i');
var clear_ctrl = g('.ctrl-i');
//5.3清除他们的active样式
for(i=0;i<clear_ctrl.length;i++){
clear_main[i].className = clear_main[i].className.replace('main-i_active','');
clear_ctrl[i].className = clear_ctrl[i].className.replace('ctrl-i_active','');
}
for(i=0;i<clear_ctrl.length; i++ ){ //标准的遍历语法,真正意义上的数组对象可以用 for in循环。
clear_main[i].className = clear_main[i].className
.replace(' main-i_active','');
clear_ctrl[i].className = clear_ctrl[i].className
.replace(' ctrl-i_active','');
}
//5.4为当前控制按钮和图片加样式
main.className += ' main-i_active';
ctrl.className += ' ctrl-i_active';
//7.2切换时复制上一张幻灯片到 main_background 中
setTimeout(function(){
g('main_background').innerHTML = main.innerHTML;
},1000)
}
//6.动态调整图片的 margin-top ,使其垂直居中
function movePictures(){
var pictures = g('.picture');
for(i=0; i<pictures.length; i++){
pictures[i].style.marginTop = (-1 * pictures[i].clientHeight/2) + 'px';
}
}
//4.定义何时处理幻灯片输出
window.onload = function(){
addSliders();
switchSlider(2);
setTimeout(function(){
movePictures();
},100);
}
</script>
</body>
</html>
//弄了一下午终于好了。在我电脑上显示是可以的。
<!DOCTYPE html>
<html>
<title>预览图幻灯片</title>
<meta charset="utf-8">
<head>
<style>
*{margin:0;padding:0;}
body{
margin:50px 0;
font-size: 54px;
background-color: #FFF;
color:#555;
-webkit-font-smoothing:antialiased;/*字体抗锯齿*/
}
.slider{
width:100%;
height:400px;
position:relative;
}
/*幻灯片区域*/
.slider .main{
width:100%;
height:400px;
position:relative;
overflow: hidden;}
/*每一个幻灯片样式*/
.slider .main .main-i{
width:100%;
height:400px;
position:relative;}
.slider .main .main-i img{
width:100%;
position:absolute;
left:0;
top:0;
z-index:1;
}
.slider .main .main-i .caption{
position:absolute;
right:50%;
top:30%;
z-index: 9;
}
.slider .main .main-i .caption h2{
font-size: 40px;
line-height:50px;
text-align: right;
color:#B5B5B5;
}
.slider .main .main-i .caption h3{
font-size: 70px;
line-height:70px;
text-align: right;
color:#000000;
}
/*控制按钮预览图区域*/
.slider .ctrl{
width:100%;
height:13px;
line-height: 13px;
text-align: center;
position:absolute;
left:0;
bottom:-1px;
}
.slider .ctrl .ctrl-i{
display: inline-block;
width:150px;
height:13px;
background:#666;
box-shadow: 0 1px 1px,rgba(0,0,0,0.3)inset;
position:relative;
margin-left: 1px;
}
.slider .ctrl .ctrl-i:hover{background:#F0F0F0;}
.slider .ctrl .ctrl-i img{
width:100%;/*实际上它会跟随它的父元素宽度变为150px */
position:absolute;
left:0;
bottom:50px;
z-index:2;
opacity:0;
-webkit-transition:all 0.2s;}
.slider .ctrl .ctrl-i:hover img{bottom:13px;
-webkit-box-reflect:below 0px -webkit-gradient(
linear,
left top,
left bottom,
from(transparent),
color-stop(50%,transparent),
to(rgba(255,255,255,0.3)));
opacity:1;}
/*当前展现的状态*/
.slider .ctrl .ctrl-i_active:hover,
.slider .ctrl .ctrl-i_active{
background:#000;
}
.slider .ctrl .ctrl-i_active:hover img{
opacity:0;
}
/*幻灯片切换的样式*/
.slider .main .main-i{opacity:0;position:absolute;right:50%;top:0;-webkit-transition:all 0.5s;}
.slider .main .main-i h2{margin-right: 45px;}
.slider .main .main-i h3{margin-right:-45px;}
.slider .main .main-i h2,.slider .main .main-i h3{opacity:0;-webkit-transition:all 1s 0.8s;}
.slider .main .main-i_active{opacity:1;right:0;}
.slider .main .main-i_active h2,.slider .main .main-i_active h3{margin-right:0px;opacity:1;}
.slider .main .main-i .caption{margin-right:13%;}
</style>
</head>
<body>
<div class="slider">
<!-- 修改VIEW为模板,关键字替换 -->
<div class="main" id="template_main">
<div class="main-i" id="main_{{index}}">
<div class=caption>
<h2>{{h2}}</h2>
<h3>{{h3}}</h3>
</div>
<img src="images/{{index}}.jpg" />
</div>
</div>
<div class="ctrl" id="template_ctrl">
<a class="ctrl-i" id="ctrl_{{index}}" href="javascript:switchSlider({{index}});"><img src="images/{{index}}.jpg"/></a>
</div>
</div>
<script>
//1.数据定义(实际生产环境中,应有后台给出)
var data = [
{img:1,h1:'Creative',h2:'DUET'},
{img:2,h1:'Friendly',h2:'DEVIL'},
{img:3,h1:'Tranquilent',h2:'COMPATRIOT'},
{img:4,h1:'Insecure',h2:'HUSSLER'},
{img:5,h1:'Loving',h2:'REBEL'},
{img:6,h1:'Passionate',h2:'SEEKER'},
{img:7,h1:'Crazy',h2:'FRIEND'}
];
//2.通用函数
var g = function(id){
if(id.substr(0,1) == "."){
return document.getElementsByClassName(id.substr(1));
}
return document.getElementById(id);
}
//3.添加所有幻灯片&对应按钮
function addSliders(){
//3.1获取模板
var tpl_main= g('template_main').innerHTML
.replace(/^\s*/,'')
.replace(/\s*$/,'');
var tpl_ctrl= g('template_ctrl').innerHTML
.replace(/^\s*/,'')
.replace(/\s*$/,'');
//3.2定义最终输出 html的变量
var out_main = []; //所有幻灯片变量
var out_ctrl = []; //所有控制按钮变量
//3.3遍历所有数据,构建最终输出的 HTML
for(i in data){
var _html_main = tpl_main
.replace(/{{index}}/g,data[i].img)
.replace(/{{h2}}/g,data[i].h1)
.replace(/{{h3}}/g,data[i].h2)
.replace(/{{css}}/g,['','main-i_right'][i%2]);
var _html_ctrl = tpl_ctrl
.replace(/{{index}}/g,data[i].img);
out_main.push(_html_main);
out_ctrl.push(_html_ctrl);
}
//3.4吧HTML回写到对应的DOM里面
g('template_main').innerHTML = out_main.join('');
g('template_ctrl').innerHTML = out_ctrl.join('');
//7.增加 #main_background,以免出现空白背景
g('template_main').innerHTML += tpl_main
.replace(/{{index}}/g,'{{index}}');
g('main_{{index}}').id = 'main_background';
}
//5.幻灯片切换
function switchSlider(n){
//5.1获得要展现的幻丁片&控制按钮 DOM
var main = g('main_'+n);
var ctrl = g('ctrl_'+n);
//5.2获得所有幻灯片以及控制按钮
var clear_main = g('.main-i');
var clear_ctrl = g('.ctrl-i');
//5.3清除他们的active样式
for(i=0;i<clear_ctrl.length;i++){
clear_main[i].className = clear_main[i].className.replace('main-i_active','');
clear_ctrl[i].className = clear_ctrl[i].className.replace('ctrl-i_active','');
}
for(i=0;i<clear_ctrl.length; i++ ){ //标准的遍历语法,真正意义上的数组对象可以用 for in循环。
clear_main[i].className = clear_main[i].className
.replace(' main-i_active','');
clear_ctrl[i].className = clear_ctrl[i].className
.replace(' ctrl-i_active','');
}
//5.4为当前控制按钮和图片加样式
main.className += ' main-i_active';
ctrl.className += ' ctrl-i_active';
//7.2切换时复制上一张幻灯片到 main_background 中
setTimeout(function(){
g('main_background').innerHTML = main.innerHTML;
},1000)
}
//6.动态调整图片的 margin-top ,使其垂直居中
function movePictures(){
var pictures = g('.picture');
for(i=0; i<pictures.length; i++){
pictures[i].style.marginTop = (-1 * pictures[i].clientHeight/2) + 'px';
}
}
//4.定义何时处理幻灯片输出
window.onload = function(){
addSliders();
switchSlider(2);
setTimeout(function(){
movePictures();
},100);
}
</script>
</body>
</html>
举报