1.4中类型(grid_type)的卡片:1:大方块;2:竖长块;3:横长块;4:小方块

var order = 0;

// 创建链表
function List(head) {
this.head = new Node(head);
this.insert = insert;
this.find = find;
this.getNeed = getNeed;
this.position = position;
this.node = 0;
} // 创建节点
function Node(data) {
this.pre = null;
this.next = null;
this.type = data.grid_type;
this.need = [];
this.data = data;
this.arr = [[data]];
this.size = data.grid_type === 3 ? 2 : data.grid_type;
this.top = 0;
this.left = 0;
this.index = order;
} // 查找节点
function find(data, cur) {
while (!cur.need.includes(data.grid_type)) {
if (cur.next === null) {
return this.insert(cur, data);
} else {
cur = cur.next;
}
}
// 将数据添加到arr后,清空need对应得grid_type
// 如果前两个盒子是type1,那么第三个盒子不能是type2(竖方块)
if (
cur.data.grid_type === 1 &&
cur.arr[1] &&
cur.arr[1][0].grid_type === 1 &&
data.grid_type === 2
) {
if (cur.next === null) {
return this.insert(cur, data);
} else {
this.find(data, cur.next);
}
}
// 如果前一个盒子是type1,那么第二个盒子不能是type3(横方块)
if (cur.data.grid_type === 1 && data.grid_type === 3) {
if (cur.next === null) {
return this.insert(cur, data);
} else {
this.find(data, cur.next);
}
}
let size = cur.size;
let num = data.grid_type === 3 ? 2 : data.grid_type;
let total = size + num;
if (total <= 4) {
let len = cur.arr.length;
cur.arr[len] = [];
cur.arr[len][0] = data; cur.size += num;
let index = cur.need.indexOf(data.grid_type);
if (total === 4) {
cur.need = [];
} else {
cur.need.splice(index, 1);
}
} else {
return this.insert(cur, data);
}
} // 插入节点
function insert(cur, data) {
let newOne = new Node(data);
newOne.next = cur.next;
newOne.pre = cur;
cur.next = newOne;
newOne.need = this.getNeed(data);
this.node++;
newOne.index = this.node;
} // 需要得盒子
function getNeed(data) {
let type = data.grid_type;
if (type === 1) {
return [1, 1, 1, 3];
} else if (type === 2) {
return [1, 1, 2];
} else if (type === 3) {
return [1, 1, 3];
} else {
return [];
}
} // 位置信息
//half总数据得一半
function position(half) {
let cur = this.head;
let top = 0;
let box = [];
while (cur.next !== null) {
if (cur.index < half) {
p(cur.arr, cur.index, top);
box.push(cur.arr);
cur = cur.next;
} else {
top = 388;
let index = cur.index - half; //剩下得部分
p(cur.arr, index, top);
box.push(cur.arr);
cur = cur.next;
}
}
return box;
} /*
arr[[{data},{position}],[]]
*/
function p(arr, index, baseTop) {
let len = arr.length;
let base = index * 388;
arr.map(function(item, i) {
arr[i][1] = {};
arr[i][1].top = baseTop;
});
arr[0][1].left = base;
if (len === 4) {
arr[1][1].left = base + 194;
arr[2][1].left = base;
arr[2][1].top = baseTop + 194;
arr[3][1].top = baseTop + 194;
arr[3][1].left = base + 194;
} else if (len === 2) {
let flag = "";
arr.map(function(item, i) {
if (item[0].grid_type === 2) {
flag = "v";
} else if (item[0].grid_type === 3) {
flag = "h";
} else {
flag = "s";
}
});
if (flag == "s" || flag == "v") {
arr[1][1].left = base + 174;
} else if (flag === "h") {
arr[1][1].top = baseTop + 194;
arr[1][1].left = base;
}
} else if (len === 3) {
if (arr[0][0].grid_type === 1) {
if (arr[1][0].grid_type === 1) {
arr[1][1].left = base + 194;
arr[2][1].left = base;
arr[2][1].top = baseTop + 194;
} else {
arr[1][1].left = base;
arr[2][1].left = base;
arr[2][1].top = baseTop + 194;
}
} else if (arr[0][0].grid_type === 2) {
arr[1][1].left = index * 388 + 194;
arr[2][1].left = index * 388 + 194;
arr[2][1].top = baseTop + 194;
} else if (arr[0][0].grid_type === 3) {
arr[1][1].left = index * 388;
arr[1][1].top = baseTop + 194;
arr[2][1].left = index * 388 + 194;
arr[2][1].top = baseTop + 194;
}
}
} function list(data) {
let newList = new List(data[0]);
newList.head.need = newList.getNeed(newList.head.data);
for (var i = 1; i < data.length; i++) {
newList.find(data[i], newList.head);
}
let half = Math.ceil(newList.node / 2);
newList.position(half);
let finalData = allArr(newList.head); return finalData;
} function allArr(head) {
let finalData = [];
let cur = head;
while (cur.next !== null) {
finalData.push(cur.arr);
cur = cur.next;
}
return finalData;
} export default list;

双向链表--首页大小不一卡片排序 --- react --- js的更多相关文章

  1. 再谈React.js实现原生js拖拽效果

    前几天写的那个拖拽,自己留下的疑问...这次在热心博友的提示下又修正了一些小小的bug,也加了拖拽的边缘检测部分...就再聊聊拖拽吧 一.不要直接操作dom元素 react中使用了虚拟dom的概念,目 ...

  2. react.js 你应知道的9件事

    React.js 初学者应该知道的 9 件事   本文假定你已经有了一下基本的概念.如果你不熟悉 component.props 或者 state 这些名词,你最好先去阅读下官方起步和手册.下面的代码 ...

  3. React.js学习

    React.js学习之环境搭建 1 工欲善其事必先利其器:前端开发工具 1.1 WebStorm和Sublime Text Sublime Text:作为代码编辑器,Sublime Text的优点如下 ...

  4. php文件管理,能够点击依照时间,大小,名称排序

    php文件管理.能够点击依照时间.大小,名称排序  本例没实用到jquery 演示   PHP Code <?php    $rootdir="./";    $spacen ...

  5. React JS和React-Native学习指南

    自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull requests! React-Native学习指南本指南汇集React- ...

  6. React.js 入门与实战之开发适配PC端及移动端新闻头条平台课程上线了

    原文发表于我的技术博客 我在慕课网的「React.js 入门与实战之开发适配PC端及移动端新闻头条平台」课程已经上线了,文章中是目前整个课程的大纲,以后此课程还会保持持续更新,此大纲文档也会保持更新, ...

  7. React.js 小书 Lesson22 - props.children 和容器类组件

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson22 转载请注明出处,保留原文链接和作者信息. 有一类组件,充当了容器的作用,它定义了一种外层 ...

  8. React.js基础知识

    一. react.js的基本使用方法 (1)快速使用,hello world <div id="app"></div> <script src=&qu ...

  9. Web框架概述——React.js

    目前,在前端Web开发中,三大热门框架为React.js,Vue.js,Angular.js .当然,三大框架各有各的优缺点,这里就不多说了,下面我就针对前段时间所学的React框架做一下整体知识点的 ...

随机推荐

  1. Qt的QVariant类

    QStandardItemModel类中的函数 bool setData(const QModelIndex &index, const QVariant &value, int ro ...

  2. 在docker 容器中安装命令

    apt-get update ##跟新 //vi apt install vim //weget apt install weget //yum apt install yum //ifconfig ...

  3. 解决ios下的微信页面背景音乐无法自动播放问题

    在做各种html5场景页面的时候,插入背景音乐是一个很普遍的需求,我们都知道,ios下的safari是无法自动播放音乐的,以至于现在行程一种认知,ios是没有办法自动播放媒体资源的,这个认知其实是错误 ...

  4. 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格惰性加载节点

    jQuery EasyUI 树形菜单 - 树形网格惰性加载节点 有时我们已经得到充分的分层树形网格(TreeGrid)的数据. 我们还想让树形网格(TreeGrid)按层次惰性加载节点. 首先,只加载 ...

  5. Linux下编译安装MySQL

    一.环境准备yum install -y ncurses-devel libaio-develyum install -y cmake makeuseradd -s /sbin/nologin -M ...

  6. 在Winfrom 中,如何实现combox 的列表自动显示ToolTip提示 ?

    //带ToolTip的combox类文件 public class ComboBoxWithTooltip : ComboBox { //tipProperty为显示ToolTip文本的数据源的属性 ...

  7. H264编码 封装成MP4格式 视频流 RTP封包

    H264编码 封装成MP4格式 视频流 RTP封包         分类:             多媒体编程              2013-02-20 21:31     3067人阅读    ...

  8. PHP策略模式demo

    <?php//策略模式就是你有很多的方法,选择一种适合自己的,// 单例模式就是只有一个实例对象,不需要每个文件都要加载,比如连接数据库,// 工厂模式就是//策略模式interface cha ...

  9. spring boot 2.0(二)动态banner的支持

    Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...

  10. java使用线程设置定时任务

    private static int a=0; public static void main( String[] args ) { timer(); } public static void tim ...