技术心得:实现“换一批”类的效果

简介: 技术心得:实现“换一批”类的效果

需求:点击换一批,显示下一页的优惠券。


功能:动态配置几个元素为一组。


原理:点击换一批的时候,记录点击的次数和需要截取的每组数据基数。然后添加到页面中。


代码:


优惠券


ul, li {


margin: 0;


padding: 0;


list-style: //代码效果参考:http://www.lyjsj.net.cn/wx/art_23518.html

none;

overflow: hidden;


}


.list {


width: 100px;


height: 40px;


float: left;


background: darkorange;


margin-right: 20px;


color: #fff;


text-align: center;


line-height: 40px;


cursor: pointer;


}


div {


width: 100%;


text-align: right;


cursor: pointer;


}


换一批


function CouponArray() {


//默认的数据


this.couponList = 【】;


// 截取后的新数据


this.couponNewList = 【】;


// 拼接字符串变量


this.couponStr = "";


// 截取第几组的开始参数


this.timeStart = 0;


// 截取//代码效果参考:http://www.lyjsj.net.cn/wz/art_23516.html

第几组的结束参数

this.timeEnd = 1;


//默认为0组


this.group = 0;


//默认5个为一组,可以根据需求修改。


this.num = 5;


//点击的次数


this.clickNum = 0;


// 计算数据的长度,共分为几组,如果不能整除则加1


this.listLen = listLen;


//计算将点击次数和开始截取的参数清空


this.clear = clear;


//动态添加数据结构


this.render = render;


//每点击一次,记录次数


this.autoIncre = autoIncre;


}


function listLen() {


var len = this.couponList.length;


this.group = len / this.num;


if (len % this.num != 0) {


this.group = parseInt(this.group) + 1;


}


}


function autoIncre() {


this.clickNum++;


this.timeStart++;


this.timeEnd++;


}


function clear() {


//如果点击此时大于当前数据的组数,则重新开始计数。


if (this.clickNum > this.group - 1) {


this.timeStart = 0;


this.timeEnd = 1;


this.clickNum = 0;


}


}


function render() {


this.couponStr = "";


//截取当前每组的数据


this.couponNewList = this.couponList.slice(this.num this.timeStart, this.num this.timeEnd);


for (var i = 0; i < this.couponNewList.length; i++) {


this.couponStr += "" + this.couponNewList【i】.name + ""


}


return this.couponStr;


}


const coupon_item = document.getElementById("coupon_item");


const exchange = document.getElementById("exchange");


const p = new CouponArray();


p.couponList = 【{name: "优惠券1"}, {name: "优惠券2"}, {name: "优惠券3"}, {name: //代码效果参考:http://www.lyjsj.net.cn/wz/art_23514.html

"优惠券4"}, {name: "优惠券5"}, {name: "优惠券6"}, {name: "优惠券7"}, {name: "优惠券8"}, {name: "优惠券9"}】;

let str = p.render();


coupon_item.innerHTML = str;


exchange.addEventListener("click", function () {


if(p.couponList.length>4&&p.couponList.length>p.num){


//点击的时候获取分为几组


p.listLen();


//每点击一次记录点击次数


p.autoIncre();


p.clear();


str = p.render();


coupon_item.innerHTML = str;


}else{


alert("没有更多优惠券了");


}


});


最后:这个需求是做项目的时候顺便就写下的,如果有更方便的方式,可以留言交流。

相关文章
|
2月前
|
人工智能 自然语言处理 算法
数字永生源码独立部署怎么做?
数字人源码,数字人永生,数字人源码独立部署,数字人
数字永生源码独立部署怎么做?
|
8月前
|
弹性计算 监控 Oracle
JVM第一个外部种子客户/游戏行业的故事
JVM第一个外部种子客户/游戏行业的故事
35 0
软技能:代码之外的生存指南
又到了读书摸鱼的时间了,本期推荐的书籍是:《软技能:代码之外的生存指南》
144 0
软技能:代码之外的生存指南
|
人工智能 小程序
行动派:想到就做,无关乎与成功或失败,重在过程!
行动派:想到就做,无关乎与成功或失败,重在过程!
163 0
|
人工智能 算法 搜索推荐
颠覆传统RPA的实在IPA模式,如何做到真正人人可用
为什么实在智能能够率先发布一款颠覆传统RPA的IPA模式产品?CEO孙林君告诉我们答案。
190 0
颠覆传统RPA的实在IPA模式,如何做到真正人人可用
|
设计模式 JSON 算法
公司来了个京东T6,只用两个工具类教会了我如何进行数据对比
公司来了个京东T6,只用两个工具类教会了我如何进行数据对比
|
架构师 Java Spring
追逐影子的人,最终只会是影子
追逐影子的人,最终只会是影子
125 0
追逐影子的人,最终只会是影子
|
程序员
程序员离开大厂进工厂,你会是一份子吗?【每周畅聊
近日,#程序员离开大厂进工厂#、#越来越多高学历人才加入制造业#,两个话题登上热搜。数据表明,近年来制造业被年轻人所抛弃,不过最近风向标似乎有了改变:研究生、博士生开始青睐工厂,程序员也离开大厂进工厂。
|
存储 Java 程序员
终于我用JOL打破了你对java对象的所有想象
终于我用JOL打破了你对java对象的所有想象