underscore.js

js实现照片墙效果

情到浓时终转凉″ 提交于 2021-02-20 16:52:58
本次要实现的是一个照片墙的效果,如下图,很多图片随机的摆放在窗口中,当点击到某一张的时候,该张图片出现出现在窗口的水平垂直居中的位置。 首先,我们需要简单的结构处理图片,为了方便操作,引用了一个js库: underscore.js ,因为图片的数量是不固定的,这里我们采用动态添加的方式生成li,再在li里面添加图片。 <! DOCTYPE html > < html lang ="en" > < head > < meta charset ="UTF-8" > < title ></ title > < style > * { margin : 0 ; padding : 0 ; border : none ; list-style : none ; } html, body, ul { width : 100% ; height : 100% ; } #ps { position : relative ; } #ps li { width : 250px ; height : 360px ; box-shadow : 0 0 10px #000 ; } </ style > </ head > < body > < ul id ="ps" ></ ul > < script src ="js/Underscore-min.js" ></ script > < script >

JavaScript: How to group nested array

风格不统一 提交于 2021-02-19 03:50:07
问题 I am trying to display data using SectionList in React Native. I have written the code below displaying what I am trying to accomplish. I want the data to first be grouped together by date , and inside of that date, I need them grouped by location. A regular JavaScript solution will work. It's important that it has a title and data key. My input data is in this format: [ { game_id: 1171, date: '2018-11-17', location: 'Plaza' }, { game_id: 1189, date: '2018-11-17', location: 'Field - Kickball'

[JavaScript] 函数节流(throttle)和函数防抖(debounce)

帅比萌擦擦* 提交于 2021-02-14 02:32:43
js 的函数节流(throttle)和函数防抖(debounce)概述 函数防抖(debounce) 一个事件频繁触发,但是我们不想让他触发的这么频繁,于是我们就设置一个定时器让这个事件在 xxx 秒之后再执行。如果 xxx 秒内触发了,则清理定时器,重置等待事件 xxx 秒 比如在拖动 window 窗口进行 background 变色的操作的时候,如果不加限制的话,随便拖个来回会引起无限制的页面回流与重绘 或者在用户进行 input 输入的时候,对内容的验证放在用户停止输入的 300ms 后执行(当然这样不一定好,比如银行卡长度验证不能再输入过程中及时反馈) 一段代码实现窗口拖动变色 <script> let body = document.getElementsByTagName("body")[0]; let index = 0; if (body) { window.onresize = function() { index++; console.log("变色" + index + "次"); let num = [Math.ceil(Math.random() * 255), Math.ceil(Math.random() * 255), Math.ceil(Math.random() * 255)]; body.style.background = "rgb(" +

underscore how to filter all objects when there are multiple parameters

▼魔方 西西 提交于 2021-02-10 15:09:41
问题 I have a problem how to correctly filter data when it I pass one or several values ​​from filters on the page. For example I have such object with data: let data = [ {'name': 'test 1', 'address': 'New York', 'class_id': [1, 2, 3], 'country': 'USA', 'country_id': 20}, {'name': 'test 2', 'address': 'Lisbona', 'class_id': [2, 3], 'country': 'Portugal', 'country_id': 12}, {'name': 'test 3', 'address': 'New York', 'class_id': [2], 'country': 'USA', 'country_id': 20}, {'name': 'test 4', 'address':

Using Underscore's _.debounce() method

一曲冷凌霜 提交于 2021-02-07 12:33:16
问题 I'm trying to use UnderscoreJS and it's _.debounce() method to stop a callback function for firing repeatingly on keyup event. I'm doing that because each time you start typing, an AJAX call will be fired up so it would be quite expensive to make a call for each character you type (: This is how I'm using the method : onTypeEvents : function(selector, callback) { return $(selector).on('keyup', function(event){ var that = $(this).val().trim(); switch(event.keyCode) { case 8: if(that !== '') {

Issue with with 'use strict' and underscore.js

我的未来我决定 提交于 2021-02-07 06:09:44
问题 I've written an app using Yeoman and backbone.js. At the top of every js file I have specified 'use strict'; and when I run my grunt tasks jshint does not encounter any errors. I am able to build my app with grunt without issue however when I try to run the uglified js I get the following error: Uncaught SyntaxError: Strict mode code may not include a with statement I've searched the code base and the only things using a with statement is underscore. I'm new to strict mode so I'm not sure how

Issue with with 'use strict' and underscore.js

╄→尐↘猪︶ㄣ 提交于 2021-02-07 06:07:24
问题 I've written an app using Yeoman and backbone.js. At the top of every js file I have specified 'use strict'; and when I run my grunt tasks jshint does not encounter any errors. I am able to build my app with grunt without issue however when I try to run the uglified js I get the following error: Uncaught SyntaxError: Strict mode code may not include a with statement I've searched the code base and the only things using a with statement is underscore. I'm new to strict mode so I'm not sure how

Issue with with 'use strict' and underscore.js

≡放荡痞女 提交于 2021-02-07 06:07:13
问题 I've written an app using Yeoman and backbone.js. At the top of every js file I have specified 'use strict'; and when I run my grunt tasks jshint does not encounter any errors. I am able to build my app with grunt without issue however when I try to run the uglified js I get the following error: Uncaught SyntaxError: Strict mode code may not include a with statement I've searched the code base and the only things using a with statement is underscore. I'm new to strict mode so I'm not sure how

How do I shuffle a Javascript Array ensuring each Index is in a new position in the new Array?

孤者浪人 提交于 2021-02-04 17:07:40
问题 I have an Array of Objects, like so. var usersGoing = [ { user: 0 }, { user: 1 }, { user: 2 }, { user: 3 }, { user: 4 } ]; I need to shuffle this Array so that no Object remains in the same index as when it was instantiated, like so: [ { user: 3 }, { user: 2 }, { user: 4 }, { user: 0 }, { user: 1 } ] It is IMPERATIVE that the resulting array be sorted in this manner, as each of these user Objects will be assigned to a different user Object. I have tried a few different sorting algorithms,

How do I shuffle a Javascript Array ensuring each Index is in a new position in the new Array?

江枫思渺然 提交于 2021-02-04 17:07:35
问题 I have an Array of Objects, like so. var usersGoing = [ { user: 0 }, { user: 1 }, { user: 2 }, { user: 3 }, { user: 4 } ]; I need to shuffle this Array so that no Object remains in the same index as when it was instantiated, like so: [ { user: 3 }, { user: 2 }, { user: 4 }, { user: 0 }, { user: 1 } ] It is IMPERATIVE that the resulting array be sorted in this manner, as each of these user Objects will be assigned to a different user Object. I have tried a few different sorting algorithms,