首页 技术 正文
技术 2022年11月20日
0 收藏 731 点赞 2,786 浏览 5400 个字

前不久我做了一个3d仿酷狗的轮播图,用的技术原理就是简单的jquery遍历+css样式读写。

这次呢,我们换一种思路(呵呵其实换汤不换药),看到上次那个轮播吗?你有没有发现用jquery的animate来写的话(酷狗那轮播样式6个其实还好),如果很多的话呢?会怎样,是不是得写一大坨?这显然不是我们想要的,那怎么办呢?其实既然是样式嘛,那我们就可以封成一个css类,一个类名,我们动态的把这些类名给添加给对应的位置即可,代码简单很多。先上一个gif吧。

单纯的不用js的h5+css3也是可以有很强大的动画效果,不过单纯的h5+c3缺少行为交互层,还是比较推荐用js,代码也比较少!

3d轮播图(另一种方式,可以实现的功能更为强大也更为灵活,简单一句话,比酷狗优酷的炫)

这个gif不会弄。随便弄了个(不好看这不是重点)。

咱先来对比下封成类名和一个个样式来设置两者的优劣:

之前遍历样式的代码是这样的:

3d轮播图(另一种方式,可以实现的功能更为强大也更为灵活,简单一句话,比酷狗优酷的炫)

设置成类名后的遍历是这样的:

3d轮播图(另一种方式,可以实现的功能更为强大也更为灵活,简单一句话,比酷狗优酷的炫)

如果js的样式有很多种,不仅仅是上面6种的话,你会发现这些样式跟你看一串json数据没区别了,你会很累(当然这仅仅是夸张说法),代码也是一大坨,嗯对,就是一大坨,你能忍么?反正我不能忍。js的函数式编程一直与优雅不沾边,当然后续的ES6和ES7,特别是模块化思想的引入,其实整体上好很多了,以后我们也是可以优雅而又严谨的写代码= =

同样的还是上一下demo吧:

html+js

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>名师风采</title>
<link rel="stylesheet" href="css/dc-carousel.css" rel="external nofollow" >
<script src="js/jquery-3.1.1.min.js"></script>
<script>
$(function(){
var $dc_img=$('.dc_carousel_img li');
var $dc_point=$('.dc_carousel_point li');
var $dc_left=$('.previous');
var $dc_right=$('.next');
var nowTime=0,index=0,my;
function dc_list(i){
$dc_point.eq(i).addClass("current_point").siblings().removeClass('current_point');
} var dc_class=[];
//动态修正class
function slide(flag){
$dc_img.each(function(i){
dc_class[i]=$(this).attr("class");
});
console.log(dc_class);
$dc_img.each(function(i){
console.log($(this).attr("class"));
if(flag){
var a=i+1;
a%=$dc_img.length;
}
else{
var a=i-1;
a<0?a=$dc_img.length-1:a;
}
$(this).css("transition","").removeClass();
if(dc_class[a]!="others"){
// &&dc_class[a-1]!="others"
$(this).css("transition","0.5s");
}
$(this).addClass(dc_class[a]);
})
}
//左右变动函数
function dc_leftImg(){
if(new Date()-nowTime>500){
nowTime=new Date();
index--;
index<0?index=$dc_point.length-1:index;
dc_list(index);
slide(false);
}
}
function dc_rightImg(){
if(new Date()-nowTime>500){
nowTime=new Date();
index++;
index=index%$dc_point.length;
dc_list(index);
slide(true);
}
}
//左右按钮点击函数
$dc_left.click(function(){
dc_leftImg();
autoPlay(false);
});
$dc_right.click(function(){
dc_rightImg();
autoPlay(true);
});
/**
* 自动播放函数
* */
var time;
function autoPlay(flag){
clearInterval(time);
if(flag){
time=setInterval(dc_rightImg,2000);
}
else{
time=setInterval(dc_leftImg,2000);
}
my=autoPlay.arguments[0];
}
autoPlay(true); $dc_img.mouseover(function(){
clearInterval(time);
console.log($(this));
});
$dc_img.mouseout(function(){
autoPlay(my);
}); })
</script>
</head>
<body>
<div class="box">
<div class="dc_carousel" id="dc_carousel">
<div class="dc_carousel_img">
<ul>
<li class="first"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher1.jpg" alt=""></a></li>
<li class="second"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher2.jpg" alt=""></a></li>
<li class="third"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher3.jpg" alt=""></a></li>
<li class="others"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher4.jpg" alt=""></a></li>
<li class="others"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher7.jpg" alt=""></a></li>
<li class="others"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher8.jpg" alt=""></a></li>
<li class="others"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher9.jpg" alt=""></a></li>
<li class="last_second"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher5.jpg" alt=""></a></li>
<li class="last"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="data:images/teacher6.jpg" alt=""></a></li>
</ul>
</div>
<div class="dc_carousel_point">
<ul>
<li class="current_point"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<span class="previous dc_left"><img src="data:images/left.png" alt=""></span>
<span class="next dc_right"><img src="data:images/right.png" alt=""></span>
</div>
</div>
</body>
</html>

css

html,body,div,ol,ul,li,p,h1,h2,h3,h4,h5,h6,table,dt,dd,span,input,img{margin:;padding:}ul,li{list-style:none}a{text-decoration:none}.dc_left{float:left}.dc_right{float:right}.dc_clear{content:"";display:block;clear:both}.box{width:800px;height:300px;margin:20px auto 0;box-shadow:0 0 10px #000;-webkit-box-shadow:0 0 10px #000;-moz-box-shadow:0 0 10px #000;-o-box-shadow:0 0 10px #000;position:relative}.box .dc_carousel{width:100%;height:100%}.box .dc_carousel .dc_carousel_img{width:100%;height:90%;overflow:hidden;position:relative}.box .dc_carousel .dc_carousel_img ul{width:100%;height:100%;transform-style:preserve-3d;perspective:600px}.box .dc_carousel .dc_carousel_img ul li{width:20%;height:90%;position:absolute;box-shadow:-5px 5px 15px #333;-webkit-box-shadow:-5px 5px 15px #333;-moz-box-shadow:-5px 5px 15px #333;-o-box-shadow:-5px 5px 15px #333;opacity:}.box .dc_carousel .dc_carousel_img ul li img{width:100%;height:100%}.box .dc_carousel .dc_carousel_img ul .others{top:-40%}.box .dc_carousel .dc_carousel_img ul .first{left:40%;opacity:}.box .dc_carousel .dc_carousel_img ul .second{left:60%;transform:rotateY(40deg) scale(.5);opacity:}.box .dc_carousel .dc_carousel_img ul .last{left:20%;transform:rotateY(-40deg) scale(.5);opacity:}.box .dc_carousel .dc_carousel_img ul .third{left:70%;transform:rotateY(50deg) scale(.2);opacity:}.box .dc_carousel .dc_carousel_img ul .last_second{left:10%;transform:rotateY(-50deg) scale(.2);opacity:}.box .dc_carousel .dc_carousel_img ul .dc_change{transform:translate3d(30px, 30px, 30px)}.box .dc_carousel .dc_carousel_point{width:100%;text-align:center;position:absolute;z-index:}.box .dc_carousel .dc_carousel_point li{width:10px;height:10px;list-style:none;border-radius:50%;background:#bbb;display:inline-block}.box .dc_carousel .dc_carousel_point .current_point{background:#3ff}.box .dc_carousel .previous,.box .dc_carousel .next{position:absolute;top:40%;font:30px "Times New Roman";cursor:pointer}.box .dc_carousel .next{left:94%}

跟上一次的原理类似,但这个不同的是用类名来代替并动态修改类名,可拓展性更强,且修改只需修改css样式即可,简单快捷方便,另外一个是图片是可以无限拓展的,不过需要把新加的图片放在中间并加上others类名,很方便吧~~

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,762
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838