有注意到每次实作练习的最前面都会有个-观察功能需求项目吗?这是在馒头计画中老师特别强调要拉出来制作的部分,後来看其他技术文或是六角学院课程也会提到这部分的重要性,在开始一个作品前,都要先去了解要制作哪些项目,将它拆解,并大约知道可以用什麽方式来制作,可以快速进入状况、减少紧张感并依据整理出的项目一项项完成,即便中断也能快速衔接上未完成的部分。
1.随着页面的往下卷动,会载入新的资料
2.网页的各资料是有顺序,资料左上会有编码
$.ajax
抓取API资料的方式,如url、type、dataTypelet limit = 5;let page = 1
於url中加入该变数$.each
来执行
$.each
先传入该资料(data)阵列,其function (index, value)
放入索引以及值的内容。postEl
变数,创造名为post
的div
=> $('\<div />')
post
classpostEl.appendTo('#posts-container');
let limit = 5
let page = 1
async function doAjax() {
let result;
try {
result = await $.ajax({
url: `https://jsonplaceholder.typicode.com/posts?_limit=${limit}&_page=${page}`,
type: 'get',
dataType: 'json',
success: function (data) {
$.each(data, function (index, value) {
const postEl = $('<div />').addClass('post').html(`<div class="number">${value.id}</div> <div class="post-info"><h2 class="post-title">${value.title}</h2><p class="post-body">${value.body}</p>
</div>`)
//console.log(postEl)
postEl.appendTo('#posts-container');
//$('#posts-container').append(postEl)
});
}
});
return result;
} catch (error) {
console.error(error);
}
}
doAjax();
查看是否有成功取得资料:console.log(data);
参考资料:
使用jQuery创造div
https://stackoverflow.com/questions/10402567/jquery-div-vs-div
jQuery.each()
scrollTop
网页右边的卷轴到最上端网页的距离有多少scrollHeight
取得整个没被挡住的高、clientHeight
取得元素的高度(含padding 不含 border)例子:
$(function () {
$(window).scroll(function () {
var scrollVal = $(this).scrollTop();
$("span.qScrollTop").text(scrollVal);
});
});
if(scrollVal > 500){
/* 如果滚动的物件卷动 > 500 则触发指定的动作。*/
}
scrollTop + clientHeight >= scrollHeight - 5
来计算,当卷轴卷到该位置时,要呈现载入画面setTimeout()
,在1秒後消除载入图示,接着在300毫秒後,马上换页执行载入新资料$(window).scroll(function () {
var scrollTop = $(this).scrollTop();
var scrollHeight = $('body').prop("scrollHeight");
//一样 var scrollHeight2 = document.documentElement.scrollHeight;
var clientHeight = document.documentElement.clientHeight;
//https://stackoverflow.com/questions/10423759/plain-javascript-to-jquery-clientheight
// console.log('scrollTop:', scrollTop);
// console.log('scrollHeight:', scrollHeight);
// console.log('clientHeight:', clientHeight);
if (scrollTop + clientHeight >= scrollHeight - 5) {
//console.log('show up 123')
showLoading();
}
})
//显示载入图示,并取得更多串接资料
function showLoading() {
$('.loader').addClass('show');
setTimeout(function () {
$('.loader').removeClass('show');
setTimeout(function () {
page++;
doAjax();
}, 300);
}, 1000); //1秒之後消失
}
参考资料:
[笔记] 计算网页底部位置,当网页达到底部时才产生效果─jQuery
一次搞懂 clientHeight/scrollHeight/scrollTop的区别
.keyup
指放开键盘的那个刹那,触发该事件var text
取得输入值并转为小写indexOf()
方法用来判断字串字串变数中是否包含某字串。//输入框搜寻//https://makitweb.com/jquery-search-text-in-the-element-with-contains-selector/ (=>Loop all .content )
$('#filter').keyup(function () {
// 输入值的搜寻
var text = $('#filter').val().toLowerCase();
// 隐藏所有资料
$('.post').hide();
// 回圈搜寻整个文件
$('.post').each(function () {
if ($(this).text().toLowerCase().indexOf("" + text + "") != -1) {
$(this).closest('.post').show();
}
});
参考资料:
比较 keydown, keypress, keyup 的差异
jQuery – Search text in the Element with :contains() Selector
JavaScript String indexOf()
>>: Day 23 资料宝石:【Lab】RDS架构 建立自己的第一台云端资料库 (下)
前情提要: 工具人揭露了残酷事实,JS有一个青梅竹马的存在! 我: 到底是怎麽样的人物? 工具人们:...
1.前言 这边主要是为解说前几篇关於AHT10的程序码,此次主要讲的部分是loop中的程序码,因为透...
燃烧溶解文字 教学原文参考:燃烧溶解文字 这篇文章会介绍在 GIMP 里使用图层合并、遮罩、文字,搭...
在 Flask 里面导入 request 套件包 from flask import request...
上一篇介绍了Vito'sfamily,这一题题目讲那麽多,但其实主要是考我们找出中位数,并让每个数都...