无论你想走多远,你都需要不断地走下去。前端最精华的便是原生的js,这也是我们前端工程师的技术分层的重要指标,也提现这你的代码能力,开发的水平。废话不多说,进入今天的主要分享————基于面向对象思想的图片轮播。其效果如下所示:

正如图片所示这样我们要实现的效果,这里与我们以往不同的是我们的布局十分简洁。其中html布局如下:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="index.css">
<script src="tools.js"></script>
<script src="js.js"></script>
</head>
<body>
<div class="container" id="slide1">
<ul>
<li><a href=""><img src="data:images/1.jpg" alt="文字1"></a></li>
<li><a href=""><img src="data:images/2.jpg" alt="文字2"></a></li>
<li><a href=""><img src="data:images/3.jpg" alt="文字3"></a></li>
<li><a href=""><img src="data:images/4.jpg" alt="文字4"></a></li>
</ul>
</div>
<script>
var s = new Slider("slide1");
console.info(s);
</script>
</body>
</html>

样式文件index.css

 ul,ol,li{padding:;margin:;list-style: none;}
.container{
width: 500px;
height: 300px;
margin:50px auto;
position: relative;
}
#msg{
width:100%;
height:40px;
line-height: 40px;
text-indent:10px;
position:absolute;
bottom:0px;
left:;
color:white;
font-size: 16px;
background:rgba(0,0,0,.8);
cursor:pointer;
z-index:;
}
ul li a{display: block;}
img{width: 500px;height: 300px;}
ol{position: absolute;bottom:10px; left:50%; -webkit-transform:translateX(-50%); background:rgba(255,255,255,.6);border-radius:7px;padding:3px;z-index:;}
ol li{background: red; float:left; width:8px; height:8px;margin-left:5px; -webkit-border-radius:4px;-mz-border-radius:4px;margin-right: 7px}
span{
width:30px;
height:45px;
line-height: 45px;
font-size:40px;
color:white;
background:rgba(255,255,255,.3);
cursor:pointer;
position:absolute;
font-weight: bold;
top:50%;
left:;
-webkit-transform:translateY(-50%);
-mz-transform:translateY(-50%);
-webkit-transition:all 1s ease 0s;
}
#rtBtn{
right:;
left:auto;
}
span:hover{
-webkit-transform:rotateY(40deg) translateX(-30px) scale(1.2);
}

注意这里的浏览器兼容,我这里只兼容了内核为-webkit-的;同时注意这里的ol和div的样式设计。

js.js代码:

 function Slider(containerId){
this.containerId = $(containerId);
this.ullis =$get($get(containerId,"ul")[0],"li");
this.num = this.ullis.length;
this.ollis =this.createList();
this.indexA = 1;
this.timer;
this.init(this.indexA);
this.ltBtn = $("ltBtn");
this.rtBtn = $("rtBtn");
this.mouseenter();
this.autoplay(this.indexA);
console.info(this.ollis);
}
Slider.prototype.createList =function(){//创建ol
var ol = $create("ol");
var lis = [];
for(var i = 0; i<this.num;i++){
var li = $create("li");
ol.appendChild(li);
lis.push(li);
}
this.containerId.appendChild(ol);
var spanleft = $create("span")
spanleft.innerHTML="&lt;"
spanleft.id="ltBtn";
this.containerId.appendChild(spanleft);
var spanright =$create("span");
spanright.innerHTML = "&gt;"
spanright.id = "rtBtn";
this.containerId.appendChild(spanright);
var div = $create("div")
div.id= "msg";
this.containerId.appendChild(div);
return lis;
}
Slider.prototype.init =function(index){
this.moveto(index);
}
Slider.prototype.mouseenter =function(){
var that = this;
for (var i = 0; i < this.num; i++) {
this.ollis[i].index = i;
this.ollis[i].onmouseenter = function(){
that.moveto(this.index);
}
}
this.ltBtn.onclick = function(){
if(that.indexA>0){
that.indexA--;
that.moveto(that.indexA);
}else{
that.indexA=that.num-1;
that.moveto(that.indexA);
}
}
this.rtBtn.onclick = function(){
if(that.indexA<that.num-1){
that.indexA++;
that.moveto(that.indexA);
}else{
that.indexA = 0;
that.moveto(that.indexA);
}
}
}
Slider.prototype.moveto =function(index){
for (var i = 0; i < this.num; i++) {
this.ullis[i].style.display="none";
this.ollis[i].style.backgroundColor="red";
}
this.ullis[index].style.display="block";
this.ollis[index].style.backgroundColor="blue";
$("msg").innerHTML = this.ullis[index].firstChild.firstChild.alt;
}
Slider.prototype.autoplay= function(indexA){
var that =this; that.timer = setInterval(function(){
indexA%=that.num;
that.moveto(indexA);
indexA++;
},3000);
that.containerId.onmouseenter =function(){
clearInterval(that.timer);
console.info(that.containerId);
}
that.containerId.onmouseleave = function(){
that.autoplay(indexA)
} }

简单讲一下:此次开发的大致思想,首先我们在html中首先生成一个对象构造器——即Slider,这样的好处是,我们以后可以很方便地通过我们的构造器来实现一个图片轮播的效果,减少代码冗余,同时减少了变量命名的冲突;当然它也有弊端,对于小型项目不是很好,所以在开发工程需要考虑。

此次开发中,我们需要首先实现:html架构和简单的样式操作,这里我们只需要下次添加我们需要的图片轮播的图片资源和整个容器div就可以了,减少了对应的轮播导航下标的编写,提现了代码的简单性,更适合小白类开发的编写。

这里遇到的问题有:

1.如何创建一个元素,如何给创建的元素添加到需要的父亲结点上

2.如何给生成的标签添加类名,以及属性

解决办法

1.利用 var li =document.creatElement("li")来创建新的属性标签,即dom操作。

创建dom元素(createElement()创建元素,createTextNode()创建文本结点)并且添加 appendChild()示例如下:

<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div> <script>
var para=document.createElement("p");
var node=document.createTextNode("This is new.");
para.appendChild(node); var element=document.getElementById("div1");
element.appendChild(para);
</script>

2.属性的操作和修改:

这里对于创建的元素的类名和id进行演示

<!DOCTYPE html>
<html lang="en">
<header>
<meta charset ="utf-8">
<style>
.cur{
color:red;
}
</style>
<title>创建元素</title>
</header>
<body>
<ul id="content">
</ul>
<script>
var li = document.createElement("li");
li.classList.value = "cur";
li.innerHTML = "今天我学习面向对象开发思想";
document.getElementById("content").appendChild(li);
</script>
</body>
</html>

效果图:

前端开发中的样式设计:一般思路首先对于大型网站的开发,我们需要统一设计初始化样式,一般文件名是:base.css,其中是我们对于所有浏览器统一使用的一个文件类型,其中设计代码如下:

 @charset "utf-8";

 /* CSS reset */
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{margin:;padding:;}
table{border-collapse:collapse;border-spacing:;}
fieldset,img {border:;}
img{vertical-align: top;}
address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}
ol,ul {list-style:none;}
capation,th{text-align:left;}
h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}
q:before, q:after{content:'';}
abbr,acronym{border:;} .tl{text-align:left;}
.tc{text-align:center;}
.tr{text-align:right;}
.bc{margin-left:auto;margin-right:auto;} /*块状元素集中对齐 block center*/
.fl{float:left;display:inline;} /* 可修复IE6中双倍边距bug */
.fr{float:right;display:inline;}
.cb{clear:both;}
.overflow-h{overflow:hidden;} .clearfix:after{
content:'';
display:block;
height:;
visibility: hidden;
clear:both;
}
.clearfix{ /*IE6中的解决方案*/
zoom:;
}

注意这里我添加了一个新的类,通过伪对象的方法来解决浮动造成的塌陷问题,常用的解决浮动塌陷的方法是

1.给使用float元素的父类结束前添加一个高为0,宽为0并且有clear:both样式的div;

2.在使用float元素的父类元素添加overflow:hidden

3.使用after伪对象清除浮动,如上面的代码显示。

在项目开发中样式设计过程中,我们不会去修改其他工程师已经编写的样式,而是在他们的基础上,我们添加新的样式,来覆盖他们的样式;这样的好处,可以避免修改样式而毁坏原来的样式,而影响到项目的开发。我们常用的开发的插件,如在手机端开发常用的图片轮播插件的样式设计,我们也是在后面修改的。

一般我们会把通用的一些样式写在一个新的css文件中,提供给大家使用,把一些对应的的样式写在index.css文件中。注意这里使用了css3中新增的属性:transfrom,border-radius,rotate(),rgba()来实现背景色变化,在IE浏览器中不兼容,需要写成background:rgb(255,255,255);filter(alpha(opacity=60));今天就大致说到这里吧。

js.js文件

注意这里不是光引用这个文件就可以了,而是在html中通过var s = new Slider("id");来调用我们编写的构造器。注意在编写程序的时候,不要一开始写完了js构造器所有的代码在去调用,因为这样不方便检查我们的编写是否出现了出现了错误,这里推荐下错误检查方法:一般都老鸟都会通过console.info()或者console.log()来答应我们的变量或者传递的参数,还以可以通过打印一个变量来检测我们的代码是否执行,是否有效,常见的错误类型有语法错误,引用类型错误,逻辑错误等,所以要注意错误类型并且及时修改。

工具文件,我们可以把我们常用的方法封装成一个函数,通过传递形参的形式来获得我们需要的元素和效果,如下面所示的tools.js

function $(id){
return document.getElementById(id);
}
function $get(containerId,tagName){
if(typeof containerId =="string" && $(containerId)){
return $(containerId).getElementsByTagName(tagName);
}
else if(typeof containerId=="object")
{
return containerId.getElementsByTagName(tagName);
}
else{
throw ("你写的第一个参数不是一个ID");
}
}
function $create(TagName,attr){
var dom = document.createElement(TagName);
for(var p in attr){
dom[p] = attr[p];
}
return dom;
}

最后我们分析一下构造器中需要的属性:

1.创建对应的ol导航和对应的按钮

2.初始化所有的样式操作

3.显示在对应的容器操作

4.鼠标进入事件

5.自动播放事件

在这些基本的事件中我们需要注意调用的顺序,如:创建肯定在初始化之前。我们可以把一些通用的属性放在原型链中来编写,这样的好处减少了变量空间占用和多次访问属性的效果。

遇到的问题:

1.其中的this指代问题:这里的解决办法是在鼠标进入之前的函数中缓存一下var that = this。 这样就可以访问属性了。

2.图片轮播判断:向左点击的时候, 如果当前的索引值大于零,让它执行自减操作,如果不大于0 就让他等于对应图片长度-1;

        向右点击的时候,当前的索引值小于它对应的轮播图片的长度-1,执行自加1操作,超过图片轮播长度时,索引值等于0,代码如下所示

this.ltBtn.onclick = function(){
if(that.indexA>0){
that.indexA--;
that.moveto(that.indexA);
}else{
that.indexA=that.num-1;
that.moveto(that.indexA);
}
}
this.rtBtn.onclick = function(){
if(that.indexA<that.num-1){
that.indexA++;
that.moveto(that.indexA);
}else{
that.indexA = 0;
that.moveto(that.indexA);
}
}

最新文章

  1. IOS的七种手势
  2. Range Sum Query - Immutable
  3. postgresql 中replace 函数
  4. 【Java EE 学习 35 上】【strus2】【类型转换器】【struts2和Servlet API解耦】【国际化问题】【资源文件乱码问题已经解决】
  5. VC++ 两种动态调整控件位置的方法(CButton设置为Radio形式会出现错误)
  6. 前端安全之XSS攻击
  7. The length of the string value exceeds the length configured in the mapping/parameter.
  8. [最近公共祖先] POJ 1330 Nearest Common Ancestors
  9. springmvc常用注解与类型转换
  10. CentOS6.4 配置Nload监控网卡流量
  11. 硬盘缓存的最佳方案,DiskLruCache完全解析
  12. hdu 3746 Cyclic Nacklace KMP循环节
  13. 【转】关于C的未定义行为
  14. Maya pywin32
  15. openGl超级宝典学习笔记 (1)第一个三角形
  16. IndexAction.java (Java之负基础实战)
  17. 基于谱聚类的三维网格分割算法(Spectral Clustering)
  18. android项目数据库升级跨版本管理解决方案
  19. 如何开发自己的搜索帝国之安装ik分词器
  20. 教你如何用ps制作紫色光斑效果

热门文章

  1. JS 对象封装的常用方式
  2. PPT转化成Image、PPTX、XPS、EMF
  3. ARM CPU大小端
  4. TFS工作项数据统计及相关数据库结构分析
  5. 『.NET Core CLI工具文档』(七)dotnet-new
  6. .NET框架解决的问题
  7. C++ 最小化到托盘
  8. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(二)
  9. C#开发微信门户及应用(15)-微信菜单增加扫一扫、发图片、发地理位置功能
  10. 最大半连通子图 bzoj 1093