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 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态.点击列表的项,切 ...
随机推荐
- local_irq_disable
local_irq_disable 仅仅是 设置 当前CPU 的中断屏蔽位 disable_irq 是禁用 全部cpu 中断(只是当前irq) 如果你要禁止所有的中断该怎么办? 在2.6内核中,可以通 ...
- 有关定时器setTimeout()、setInterval()详解
JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成. setTimeout() setTimeout函数用 ...
- Microsoft.XMLHTTP基本用法
客户端调用XMLHTTP的过程很简单,只有5个步骤:1.创建XMLHTTP对象2.打开与服务端的连接,同时定义指令发送方式,服务网页(URL)和请求权限等.客户端通过Open命令打开与服务端的服务网页 ...
- codeforces 448B. Suffix Structures 解题报告
题目链接:http://codeforces.com/problemset/problem/448/B 题目意思:给出两种操作automaton:可以删除字符串中任意一个字符: array:交换字符串 ...
- Bug: CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0.0000 to 0.0000
原因是当前的scheduleOnce还没有执行完成, 可以将scheduleOnce方法改写成另外一种形式,把CCDelayTime和CCCallFunc拼接构造延迟事件调用: CCDelayTime ...
- 【旧文章搬运】Windbg+Vmware驱动调试入门(四)---VirtualKD内核调试加速工具
原文发表于百度空间,2009-01-09========================================================================== 今天又想起 ...
- 【205】C#实现远程桌面访问
参考:Remote Desktop using C#.NET 参考文件:TscForm.zip 本博客主要是讲述怎样用 .NET 平台中 Microsoft Terminal Services Cli ...
- A - Mike and Fax
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description While ...
- 010-- 开发脚本自动部署nginx_web和nfs及监控内存
1.编写脚本自动部署反向代理.web.nfs: #!/bin/bash #检测安装nginx function detection_nginx(){ if [ -f /etc/nginx/nginx. ...
- 用script标签加载
此文已由作者杨帆授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 注:经过更深入的测试,实在不好意思,这篇文章是有问题的 更改script的type属性 并不能通过src来加载 ...