js - 面向对象 - 小案例:轮播图、随机点名、选项卡、鼠标拖拽
面向对象
对象 : (黑盒子)不了解内部结构, 知道表面的各种操作.
面向对象 : 不了解原理的情况下 会使用功能 .
面向对象是一种通用思想,并非编程中能用,任何事情都能用.
编程语言的面向对象的特点:
封装 : 看不到里面的东西 , 用好表面功能.
继承 : 从父类继承一些方法 , 属性 , 子类又有一些新的特性.
多态
抽象 : 抽取一个功能里面多个核心的功能模块。
思想: 高内聚、低耦合
低耦合 :每个功能模块之间耦合度要低
高内聚 :模块内部要紧密相连
面向对象的风格 都是 从 new 开始的
js对象的分类:
宿主对象、内置对象 (Math)、内部对象( new )
构造函数: 通过 new 调用的
对象是通过构造函数构建出来的,对象用来存储数据
构造函数 -> 对象 -> 存储数据
为了区分构造函数与普通函数: 构造函数的首字母 大写
构造函数 与 普通函数的区别
普通函数如果内部没有return的时候 , 返回的是undefined
构造函数内部没有return返回值的时候,返回的构造出来的对象。
构造函数内部的this指向,指向的是当前对象。
总结:构造函数在new的时候构建对象,会在内部自动返回一个this 指向构造的对象。
面向对象实现流程:
1. 全局变量作为属性
2. 把对象的方法放在prototype
3. new实例化对象
4. this指向
function 构造函数名称 首字母大写(){
this.属性 = 属性值
//调用初始化方法
this.init();
}
构造函数名称 首字母大写 .prototype = {
//初始化方法 : 整合各个功能模块
init : function(){
//调用绑定事件模块
this.eventBind();
},
fn1 : function(){
},
fn2 : function(){
},
eventBind : function(){
}
}
new 构造函数名称 首字母大写 ();
面向对象实现简版轮播图
分析:
功能模块拆分
1: 图片移动
2:下一张的功能
3:上一张的功能
4:事件绑定模块
// 构造Banner函数
function Banner(){
this.oimgbox = document.querySelector('.imgbox'),
this.oimg = document.querySelectorAll('img'),
this.obtn = document.querySelectorAll('span'),
this.distance = this.oimg[0].offsetWidth,
this.count = 0 ;
//调用初始化模块
this.init();
}
//
Banner.prototype = {
//初始化模块
init : function(){
this.oimgbox.style.width = this.oimg.length * this.distance + 'px';
this.eventBind();
},
//图片移动模块
toimg : function(){
move(this.oimgbox,{'left': -this.distance * this.count})
},
//下一张
next : function(){
if(this.count >= this.oimg.length - 1){
this.count = 0;
}else{
this.count++;
}
this.toimg();
},
//上一张
pre : function(){
if(this.count <= 0 ){
this.count = this.oimg.length - 1;
}else{
this.count--;
}
this.toimg();
},
//事件绑定模块
eventBind : function(){
addEventListener(this.obtn[1],'click',this.next.bind(this));
addEventListener(this.obtn[0],'click',this.pre.bind(this));
}
}
new Banner();
面向对象实现选项卡效果
function Tab(){
this.obtn = document.querySelectorAll('span');
this.oarticle = document.querySelectorAll('article');
this.init();
}
Tab.prototype = {
init : function(){
this.eventBind();
},
// 清除类名
clearClass : function(){
for(let i = 0 ,k = this.obtn.length; i < k; i++){
this.obtn[i].className = '';
this.oarticle[i].className = '';
}
} ,
addClass :function(index){
this.clearClass();
this.obtn[index].className = 'active';
this.oarticle[index].className ='show';
},
eventBind : function(){
for(let i = 0, k = this.obtn.length; i<k; i++){
// this.obtn[i].addEventListener('click',this.addClass.bind(this,i));
this.obtn[i].addEventListener('click',this.addClass.bind(this,i));
}
}
}
new Tab();
随机点名
<div class="box">随机点名</div>
<span class="btn">开始</span>
1.初始化
2.文字变化 定时器
3.开始
4.结束
5.判断什么时候开始,什么时候结束
6.控制flag
6.事件绑定
<script>
var arr =['黄子韬','白敬亭','范世錡','黄景瑜','秦霄贤','彭昱畅','汪苏泷','侯明昊','秦凯旋'];
function RandomName(){
this.box = document.querySelector('.box');
this.btn = document.querySelector('.btn');
this.flag = false;
this.init();
}
RandomName.prototype = {
init : function(){
this.eventBind();
},
textChange : function(){
var _this = this;
clearInterval(this.timer);
this.timer = setInterval(function(){
//每隔一段事件生成一个下标
let num = parseInt(Math.random() * arr.length);
//根据随机的下标 取到名字 然后交给 box;
_this.box.innerHTML = arr[num];
//添加随机颜色
_this.box.style.color = randomColor();
},100)
},
//开始
startTxt : function(){
this.btn.innerHTML = '开始';
},
//暂停
stopTxt : function(){
this.btn.innerHTML = '暂停';
},
//判断是否开始
isStart : function(){
if(this.flag){
this.textChange();
this.stopTxt();
}else{
clearInterval(this.timer);
this.startTxt();
}
},
//控制flag
controlFlag : function(){
this.flag = this.flag ? false : true;
//用flag控制 开始 或 暂停
this.isStart();
},
//evntBind:
eventBind : function(){
this.btn.addEventListener('click',this.controlFlag.bind(this));
}
}
new RandomName();
</script>
鼠标拖拽
function Drag(){
this.div = document.querySelector('div');
//定义一个空变量
this.fn = null;
this.init();
}
Drag.prototype ={
init : function(){
this.eventBind();
},
//鼠标按下 获取位置
Down : function(e){
e = e || window.event;
this.x = e.offsetX;
this.y = e.offsetY;
//绑定鼠标移动事件 fn 调用 move()
document.addEventListener('mousemove',this.fn = this.Move.bind(this));
document.addEventListener('mouseup',this.Up.bind(this));
},
//鼠标移动
Move : function(e){
e = e || window.event;
let
l = e.clientX - this.x,
t = e.clientY - this.y;
this.div.style.left = l +'px';
this.div.style.top = t + 'px';
},
//鼠标抬起 绑定事件不移动
Up : function(){
document.removeEventListener('mousemove',this.fn);
},
eventBind : function(){
//鼠标按下
this.div.addEventListener('mousedown',this.Down.bind(this));
}
}
new Drag();
面向对象实现完整版轮播图
<script>
function Banner(){
this.oimgbox = document.querySelector('.imgbox');
this.oimg = document.getElementsByTagName('img');
this.obtn = document.querySelectorAll('span');
this.ocricle = document.querySelector('.circlebox');
this.osection = document.querySelector('section');
this.timer = null;
this.distance = this.oimg[0].offsetWidth;
this.count = 0;
this.init();
}
Banner.prototype = {
//初始化
init : function(){
this.clone();
this.autoplay();
this.setWidth();
this.addLi();
this.addClass();
this.eventBind();
},
setWidth : function(){
this.oimgbox.style.width = this.oimg.length * this.distance +'px';
},
//克隆图片
clone : function(){
this.firstimg = this.oimg[0].cloneNode();
this.oimgbox.appendChild(this.firstimg);
},
// 图片移动
toImg : function(){
move(this.oimgbox,{'left' : -this.distance * this.count});
},
//下一张
next : function(){
if(this.count >= this.oimg.length -1){
this.oimgbox.style.left = 0;
this.count = 1;
}else{
this.count++;
}
this.toImg();
this.clearClass();
this.oli[this.count >= this.oimg.length -1 ? 0:this.count].className = 'active';
},
//上一张
pre : function(){
if(this.count <= 0){
this.oimgbox.style.left = -this.distance *(this.oimg.length - 1) + 'px';
this.count = this.oimg.length -2;
}else{
this.count--;
}
this.toImg();
this.clearClass();
this.oli[this.count].className = 'active';
},
//定时器
autoplay : function(){
var _this = this;
clearInterval(this.timer);
this.timer = setInterval(() => {
_this.next();
}, 3000);
},
//清除定时器
clearTimer : function(){
clearInterval(this.timer);
},
//添加圆点
addLi : function(){
for(let i = 0 ;i < this.oimg.length -1; i++){
this.createLi = document.createElement('li');
this.ocricle.appendChild(this.createLi);
}
this.oli = document.getElementsByTagName('li');
this.oli[0].className = 'active';
},
//清除类名
clearClass : function(){
for(let i = 0 ,k = this.oli.length;i<k;i++){
this.oli[i].className = '';
}
},
addClass : function(){
for(let i = 0,k = this.oli.length;i<k;i++){
addEventListener(this.oli[i],'mouseover',()=>{
this.clearClass();
this.oli[i].className = 'active';
this.count = i;
this.toImg();
})
}
},
//事件调用
eventBind : function(){
addEventListener(this.obtn[0],'click',this.next.bind(this));
addEventListener(this.obtn[1],'click',this.pre.bind(this));
addEventListener(this.osection,'mouseover',this.clearTimer.bind(this));
addEventListener(this.osection,'mouseout',this.autoplay.bind(this));
}
}
new Banner();
</script>
js - 面向对象 - 小案例:轮播图、随机点名、选项卡、鼠标拖拽的更多相关文章
- 原生JS面向对象思想封装轮播图组件
原生JS面向对象思想封装轮播图组件 在前端页面开发过程中,页面中的轮播图特效很常见,因此我就想封装一个自己的原生JS的轮播图组件.有了这个需求就开始着手准备了,代码当然是以简洁为目标,轮播图的各个功能 ...
- 图解微信小程序---轮播图
图解微信小程序---轮播图 代码笔记 第一步:在页面创建swiper组件 第二步:编写js页面 注意事项:wx:for渲染我们js中的图片数组,item默认写法,获取我们的图片数组中的图片,可通过增加 ...
- 一分钟学会如何自定义小程序轮播图(蜜雪冰城Demo)
最近开发小程序项目用到了轮播图,默认的有点单调,作为后端程序员,研究一番最终实现了.本文会从思路,代码详细介绍,相信读过此文后,不管以后在开发中碰到轮播图还是需要自定义修改其他的样式都可以按这个思路解 ...
- JS原生带小白点轮播图
咱们刚刚说了js原生轮播图,现在给他加上可以随着一起走动的小圆点吧! css代码: *{ margin:0px; padding: 0px; } ul{ width: 2500px; height: ...
- js原生代码实现轮播图案例
一.轮播图是现在网站网页上最常见的效果之一,对于轮播图的功能,要求不同,效果也不同! 我们见过很多通过不同的方式,实现这一效果,但是有很多比较麻烦,而且不容易理解,兼容性也不好. 在这里分享一下,用j ...
- 微信小程序——轮播图实现
小程序的轮播图,也就是他其中的一个控件可以算是直接上代码: 这是WXML 页面 代码: <view class='carousel_div'> <swiper class=" ...
- 微信小程序------轮播图
swiper 微信小程序实现轮播图,和网站,APP的效果差不多,代码少,效率高. 先来看看效果图: 主要用swiper + swiper-item来实现 <view class='swiper' ...
- JS框架_(Bootstrap.js)实现简单的轮播图
Bootstrap框架中 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式 轮播图效果: <!DOCTYPE html> <html> <head&g ...
- js写的简单轮播图
这个轮播图代码是从网上找来的,专门找了个写法简单的,只是作为一个小练习,大概原理如下: 1.首先是图片切换2.自动播放3.调用自动播放4.移动到容器上边停止播放,离开自动播放5.移动到导航上停止播放, ...
随机推荐
- Java之IO流用法总结
Java的IO流概述:1.I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输.如读/写文件,网络通讯等.2.Java程序中,对于数据的输入/输出操作以“流( ...
- echarts设置柱状图颜色渐变及柱状图粗细大小
series: [ { name: '值', type: 'bar', stack: '总量', //设置柱状图大小 barWidth : 20, //设置柱状图渐变颜色 itemStyle: { n ...
- 【GeneXus】开发移动APP时,如何使用Canvas进行布局?
当我们开发移动端APP的时候,经常遇到一种布局方式,那就是层级的布局,比如:一张照片我想在照片的上面显示它的名称,但不影响我照片展示的布局大小,也就是这个名称是浮在照片上的,如图: 如果要实现这样的布 ...
- 【一起学源码-微服务】Hystrix 源码一:Hystrix基础原理与Demo搭建
说明 原创不易,如若转载 请标明来源! 欢迎关注本人微信公众号:壹枝花算不算浪漫 更多内容也可查看本人博客:一枝花算不算浪漫 前言 前情回顾 上一个系列文章讲解了Feign的源码,主要是Feign动态 ...
- JMeter——聚合报告
AggregateReport 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”. 对于每个请求,它统计响应信息并提供请求数,平均值,最大,最小值,错误率,大约吞吐量(以请 ...
- [题解]CSP2019 Solution - Part B
\(\text{orz}\) 一波现场 \(\text{A}\) 掉 \(\text{D1T3}\) 的神仙 D2T3 centroid Solution 考虑每个点 \(u\) 作为重心的贡献 假设 ...
- 【5min+】帮我排个队,谢谢。await Task.Yield()
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
- .net Core发布至IIS完全手册带各种踩坑
服务器环境配置 和各位大爷报告一下我的服务器环境 : Windows Server 2012 iis 8 小插曲开始: 运维大哥在昨天给了我一台新的server 0环境开始搭建 . 并且没有安装任何的 ...
- 关于selenium无法在chrome中自动播放flash的问题
最近用selenium写个小脚本,遇到flash不能自动播放问题 我遇到的情况,直接提示 请确认是否安装flash,其实已经安装,点击下载flash,然后提示是否允许. 整了好久,发现终极方法: ## ...
- python类型-序列
注:本文档主要是学习<Python核心编程(第二版)>时做的资料整理. 1.序列 序列的成员是有序排列的,并且可以通过下标偏移量访问到它的一个或者几个成员,包括字符串(普通字符串和Unic ...