jQuery中面向对象思想实现盒子内容切换

这里主要是模拟小米官网中的首页的内容模块实现的主要动态效果
布局:采用了bootstrap框架进行布局,及其其中的字体图标
html:
<!-- 内容 -->
<div class="content_box">
<h2>内容</h2>
<div class="row content_list">
<div class="col-md-3 book_box border01">
<ul>
<li><img src="https://i1.mifile.cn/a4/61e1385e-54de-48f3-8717-5e4f4b1cdd14"></li>
<li><img src="https://s01.mifile.cn/i/index/more-duokan.jpg"></li>
<li><img src="https://i1.mifile.cn/a4/5e5da924-84e3-4959-9e25-5891cdf30757"></li>
</ul>
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="glyphicon glyphicon-chevron-right"></span>
<div class="white_dian_box">
<div class="white_dian active"></div>
</div>
</div>
<div class="col-md-3 book_box border02">
<ul>
<li>
<img src="https://i1.mifile.cn/a4/xmad_15106277789889_NVKse.png">
</li>
<li>
<img src="https://i1.mifile.cn/a4/xmad_15106285428716_UgNzb.png">
</li>
<li>
<img src="https://i1.mifile.cn/a4/xmad_151062859387_ECIuV.png">
</li>
<li>
<img src="https://s01.mifile.cn/i/index/more-miui.jpg">
</li>
</ul>
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="glyphicon glyphicon-chevron-right"></span>
<div class="white_dian_box">
</div>
</div>
<div class="col-md-3 book_box border03">
<ul>
<li>
<img src="https://i1.mifile.cn/a4/xmad_15094191317724_FNyjV.png">
</li>
<li>
<img src="https://i1.mifile.cn/a4/T1czW_BXCv1R4cSCrK.png">
</li>
<li>
<img src="https://i1.mifile.cn/a4/695c909b-f541-4575-bace-d08b6465025b">
</li>
<li>
<img src="https://s01.mifile.cn/i/index/more-game.jpg">
</li>
</ul>
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="glyphicon glyphicon-chevron-right"></span>
<div class="white_dian_box">
</div>
</div>
<div class="col-md-3 book_box border04">
<ul>
<li>
<img src="https://i1.mifile.cn/a4/3332da7d-4056-4694-9548-c83b7b3af7d3">
</li>
<li>
<img src="https://i1.mifile.cn/a4/T1TkKvBCKv1R4cSCrK.png">
</li>
<li>
<img src="https://i1.mifile.cn/a4/T15VZvB5Kv1R4cSCrK.png">
</li>
<li>
<img src="https://s01.mifile.cn/i/index/more-app.jpg">
</li>
</ul>
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="glyphicon glyphicon-chevron-right"></span>
<div class="white_dian_box">
</div>
</div>
</div>
</div>
</div>
css:
/*内容盒子属性*/
.container .content_box .content_list >div{width: 275px; overflow: hidden;}
.container .content_box .content_list .book_box{padding: 0;margin-left: 15px;height: 300px;
background: white;}
.container .content_box .content_list .border01{border-top: 1px solid orange;}
.container .content_box .content_list .border02{border-top: 1px solid green;}
.container .content_box .content_list .border03{border-top: 1px solid red;}
.container .content_box .content_list .border04{border-top: 1px solid blue;}
.container .content_box .content_list .book_box:hover{transform:translate3d(0,-2px,0);
box-shadow:0px 15px 30px rgba(0,0,0,0.1);}
.container .content_box .content_list .book_box ul li{width: 275px; float: left;}
.container .content_box .content_list .book_box .glyphicon-chevron-left,
.container .content_box .content_list .book_box .glyphicon-chevron-right{position: absolute;
top: 40%; left: 0; padding: 10px;}
.container .content_box .content_list .book_box .glyphicon-chevron-right{left: 89%;}
.container .content_box .content_list .book_box .glyphicon-chevron-left:hover,
.container .content_box .content_list .book_box .glyphicon-chevron-right:hover{
background-color: #D9D9D9;color: #fff;}
/*白点属性*/
.container .content_box .content_list .book_box .white_dian_box{position: absolute; bottom: 20px;
left:30%;}
.container .content_box .content_list .book_box .white_dian{width: 8px; height: 8px;
background: #ccc; border-radius: 50%;float: left;margin-left: 20px; }
.container .content_box .content_list .book_box .active{background: #fff; border:1px solid #FF6700;}
js:代码
// 内容里的js
var change_box={
box_length:0,
box_width:275,
cur_el:'',//当前点击的盒子
cur_el_index:'',
all_el:[],//存放不同节点的长度、盒子的位置
init:function(cur_box){
this.box_length = $('.content_box .book_box li').length
$('.content_box .book_box ul').css('width',this.box_length*this.box_width)
$(cur_box).each((index,item)=>{
let li_length = $(item).find('li').length
//this.box_length = $(item).find('li').length
this.all_el.push({
box_length:li_length,
cur_index:0
})
$(item).find('ul').css('width',li_length*this.box_width)
// 创建白点
this.create_white_dian(li_length,item)
})
// console.log(this.all_el)
$(cur_box).click((event)=>{
this.cur_el = $(event.currentTarget)//可以确定是不同的book_box
this.cur_el_index = this.cur_el.index()
if($(event.target).hasClass('glyphicon-chevron-left')){
this.change_to_left()
}else if($(event.target).hasClass('glyphicon-chevron-right')){
this.change_to_right()
}else if($(event.target).hasClass('white_dian')){
this.change_to_index($(event.target).index())
}
})
},
//显示内容和白点高亮的方法
show_content:function(show_index){
this.cur_el.find('ul').css('margin-left',-(show_index*this.box_width))
this.cur_el.find('.white_dian_box>div').removeClass('active').eq(show_index).addClass('active')
},
//点击点击白点显示对应的内容
change_to_index:function(index){
this.show_content(index)
this.all_el[this.cur_el_index].cur_index = index
},
// 创建白点方法
create_white_dian:function(dian_num,item){
let dian_html = ''
for (var i = 0; i<dian_num; i++) {
let active = ''
if(i == 0){
active = 'active'
}
dian_html += '<div class="white_dian '+active+'"></div>'
}
$(item).find('.white_dian_box').html(dian_html)
},
change_to_left:function(){
let cur_el_different_index = this.all_el[this.cur_el_index]
cur_el_different_index.cur_index--
if(cur_el_different_index.cur_index<0){
cur_el_different_index.cur_index=cur_el_different_index.box_length-1
}
// this.cur_el.find('ul').css('margin-left',-(cur_el_different_index.cur_index*this.box_width))
this.show_content(cur_el_different_index.cur_index)
},
change_to_right:function(){
let cur_el_different_index = this.all_el[this.cur_el_index]
cur_el_different_index.cur_index++
if(cur_el_different_index.cur_index>=cur_el_different_index.box_length){
cur_el_different_index.cur_index=0
}
// this.cur_el.find('ul').css('margin-left',-(cur_el_different_index.cur_index*this.box_width))
// this.cur_el.find('.white_dian_box>div').removeClass('active').eq(cur_el_different_index
// .cur_index).addClass('active')
this.show_content(cur_el_different_index.cur_index)
}
}
change_box.init('.content_list .book_box')
jQuery中面向对象思想实现盒子内容切换的更多相关文章
- 2017.12.25 Java中面向对象思想的深刻理解
今日内容介绍 1.面向对象思想 2.类与对象的关系 3.局部变量和成员变量的关系 4.封装思想 5.private,this关键字 6.随机点名器 01面向对象和面向过程的思想 * A: 面向过程与面 ...
- jQuery中取消后续执行的内容
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title&g ...
- JQuery中DOM事件合成用法
jQuery有两个合成事件——hover()方法和toggle()方法 类似前面讲过的ready()方法,hover()方法和toggle()方法都属于jQuery自定义的方法. hover()方法: ...
- jQuery中样式和属性模块简单分析
1.行内样式操作 目标:扩展框架实现行内样式的增删改查 1.1 创建 css 方法 目标:实现单个样式或者多个样式的操作 1.1.1 css方法 -获取样式 注意:使用 style 属性只能获取行内样 ...
- jQuery中的事件——《锋利的JQuery》
虽然利用原生的JavaScript事件能完成一些交互,但jQuery增加并扩展了基本的事件处理机制.jQuery不仅提供了更加优雅的事件处理语法,而且极大地增强了事件处理能力. 1.加载DOM 在Ja ...
- 谈一谈原生JS中的【面向对象思想】
[重点提前说:面向对象的思想很重要!] 最近开始接触学习后台的PHP语言,在接触到PHP中的面向对象相关思想之后,突然想到之前曾接触的JS中的面向对象思想,无奈记性太差, ...
- jquery中ajax中post方法(多学习:洞悉原理,触类旁通)(函数封装思想)
jquery中ajax中post方法(多学习:洞悉原理,触类旁通)(函数封装思想) 一.总结 1.多看学习视频:洞悉原理,触类旁通, 2.函数封装:$.post(URL,data,callback); ...
- linux设备模型与内核中的面向对象思想
linux内核用C语言实现了C++面向对象的大部分特性:封装,继承,多态.在看内核的过程中,开始追寻其中的设计思想,封装.继承.多态.恰好今天又在看Linux设备模型,找了很多资料.总结如下: 1.l ...
- 使用jQuery 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态。点击列表的项,切换二级列表的显示或隐藏状态
查看本章节 查看作业目录 需求说明: 使用jQuery 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态.点击列表的项,切 ...
随机推荐
- POJ1321 棋盘问题 —— DFS回溯
题目链接:http://poj.org/problem?id=1321 棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- AndroidStudio——Android SDK
前言 安卓的SDK包,跨过长城下载好的,分享出来一下~ Android Studio版本 | 3.4.1 下载地址 微云下载地址 | 链接:https://share.weiyun.com/5rm6l ...
- Python(1)(安装与基本使用)
1.Python的下载和安装我就不废话了,百度上都有. 我安装的是Python 3.4.3 64bit 安装完之后,打开Cmd,输入Python 显示以上相同,按照百度的意思就是安装成功. 2.配置环 ...
- 书写优雅的shell脚本(插曲)- /proc
1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...
- mac上python3安装HTMLTestRunner
下载支持python3版本的HTMLTestRunner.py文件后,拷贝到python3的lib目录下 在终端输入如下命令: 将HTMLTestRunner.py文件拷贝到如下目录后,pycharm ...
- Spring 3.1新特性之一:使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换
最近在做WebMagic的后台,遇到一个问题:后台用到了数据库,本来理想情况下是用Mysql,但是为了做到开箱即用,也整合了一个嵌入式 数据库H2.这里面就有个问题了,如何用一套代码,提供对Mysql ...
- 004--linux命令tar 软硬链接
一.tar命令介绍: -c:创建一个新的tar文件 -t:列出tar文件中目录的内容 -x:从tar文件中抽取文件 -f:指定归档文件或磁带(也可能是软盘)设备(一般都要选) -v:显示所打包的文件的 ...
- Android 实用技巧 --- 命令godir (转载)
转自:http://blog.csdn.net/bigmarco/article/details/6995426 source build/envsetup.sh后可以使用很多android集成的sh ...
- emacs版本差异(转载)
emacs23 就是原装的,由 gnu 发布的 emacs.emacs-lucid 是由 ubuntu 调整过的.emacs-nox 是不带 x system 的 emacs 版本.除非只在终端下用, ...
- PAT团体程序设计天梯赛 - 模拟赛
由于本人愚笨,最后一题实在无力AC,于是只有前14题的题解Orz 总的来说,这次模拟赛的题目不算难,前14题基本上一眼就有思路,但是某些题写起来确实不太容易,编码复杂度有点高~ L1-1 N个数求和 ...