题目1:如何判断一个元素是否出现在窗口可视范围(浏览器的上边缘和下边缘之间,肉眼可视)。写一个函数 isVisible实现
function isVisible($node){
var windowHeight = $(window).height();
var scrollTop = $(window).scrollTop();
var nodeHeight = $node.height();
var offsetHeight = $node.offset().top;
if((scrollTop>offsetHeight-windowHeight)&&(scrollTop<offsetHeight+nodeHeight)){
return true;
}else{
return false;
}
}
题目2:当窗口滚动时,判断一个元素是不是出现在窗口可视范围。每次出现都在控制台打印 true 。用代码实现
var $box = $('.box');
$(window).on('scroll',function(){
print($box);
})
function print($nodes){
$nodes.each(function(){
if(isVisible($(this))){
console.log(true);
}
})
}
function isVisible($node){
var windowHeight = $(window).height();
var scrollTop = $(window).scrollTop();
var nodeHeight = $node.height();
var offsetHeight = $node.offset().top;
if((scrollTop>offsetHeight-windowHeight)&&(scrollTop<offsetHeight+nodeHeight)){
return true;
}else{
return false;
}
}
题目3:当窗口滚动时,判断一个元素是不是出现在窗口可视范围。在元素第一次出现时在控制台打印 true,以后再次出现不做任何处理。用代码实现
var $box = $('.box');
$(window).on('scroll',function(){
printOne($box);
})
function printOne($nodes){
$nodes.not('.appear').each(function(){
if(isVisible($(this))){
$(this).addClass('appear');
console.log(true);
}
})
}
function isVisible($node){
var windowHeight = $(window).height();
var scrollTop = $(window).scrollTop();
var nodeHeight = $node.height();
var offsetHeight = $node.offset().top;
if((scrollTop>offsetHeight-windowHeight)&&(scrollTop<offsetHeight+nodeHeight)){
return true;
}else{
return false;
}
}
4.图片懒加载的原理是什么?
- 一个网站需要加载的图片会很多,如果要求浏览器在用户请求打开该网站的那一瞬间,加载完所有的图片,会导致加载的时间非常长,且效果不一定好,如果遇到传输问题,有的图片甚至可能出现显示不全的情况,这样的话用户的体验就很差了。
对于这个问题,我们想到了一个解决办法:直到该图片出现在用户的视线范围内时,才加载该图片。
- 关键的步骤:
①判断图片是否可见:如果屏幕滚动的高度+窗口高度 > 图片到页面顶部的距离&&屏幕滚动的高度<图片的总高度+图片到页面顶部的距离,那么该图片即是可见的;
②转换图片的可见状态:修改图片原本的src值。我们可以给每一个图片的src设一个相同的初始值,这样只会发起一次请求,如果不设置,可能会出现“X”就很难看。当图片是可见时,再修改图片的src属性,这时,图片的内容就能被加载出来了。
5
//router.js
app.get('/getImgs', function(req, res) {
var arr = ["http://cdn.jirengu.com/book.jirengu.com/img/1.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/2.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/3.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/4.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/5.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/6.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/7.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/8.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/9.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/10.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/11.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/12.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/13.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/14.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/15.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/16.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/17.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/18.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/19.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/20.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/21.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/22.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/23.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/24.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/25.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/26.jpg",
"http://cdn.jirengu.com/book.jirengu.com/img/27.jpg"];
res.send(arr);
});
//index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>gaoyang</title>
<style>
.ct {
width: 620px;
padding: 0;
margin: 0 auto;
border: 1px solid #ccc;
}
ul {
list-style: none;
margin: 0;
padding: 0;
margin-left: -10px;
margin-bottom: -10px;
}
ul:after {
content: '';
display: block;
clear: both;
}
ul>li {
width: 200px;
height: 400px;
cursor: pointer;
float: left;
margin: 0;
padding: 0;
margin-left: 10px;
margin-bottom: 10px;
font-size: 0;
}
ul>li>img {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
/*vertical-align: bottom;*/
}
</style>
</head>
<body>
<div class="ct">
<ul class="box">
</ul>
</div>
<script src='./jquery.js'></script>
<script>
var $box = $('.box');
var $li;
var $img;
//监听滚动事件
$(window).on('scroll',function(){
loadImg($img);
})
//加载图片
function loadImg($nodes){
$nodes.not('.appear').each(function(){
if(isVisible($(this))){
$(this).addClass('appear');
//取出data-src数据
var imgUrl = $(this).attr('data-src')
console.log(imgUrl)
//赋值src
$(this).attr('src',imgUrl);
}
})
}
//判断图片是否一出现在可视范围
function isVisible($node){
var windowHeight = $(window).height();
var scrollTop = $(window).scrollTop();
var nodeHeight = $node.height();
var offsetHeight = $node.offset().top;
if((scrollTop>offsetHeight-windowHeight)&&(scrollTop<offsetHeight+nodeHeight)){
return true;
}else{
return false;
}
}
//ajax请求
$.get('/getImgs').done(function(ret){
for(var i=0 ;i<ret.length;i++){
$box.append('<li><img src="" data-src='+ret[i]+'></li>')
}
$img = $('.box>li>img');
loadImg($img);
}).fail(function(){
})
</script>
</body>
</html>
6
//router.js
app.get('/getNews', function (req, res) {
var page = parseInt(req.query.page);
var len = parseInt(req.query.len);
var arr = [];
if (page < 5) {
for (var i = page * len; i < page * len + len; i++) {
arr.push('数据' + i);
}
}
res.send(arr);
});
//index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>gaoyang</title>
<style>
.box {
list-style: none;
padding: 0;
margin: 0;
width: 600px;
}
.box>li {
border: 1px solid #ccc;
margin: 10px;
}
.box>li>a:hover {
background-color: blue;
color: #fff;
}
.box>li>a {
display: block;
text-decoration: none;
padding: 10px;
color: #000;
}
p {
visibility: hidden;
}
</style>
</head>
<body>
<div class="ct">
<ul class="box">
</ul>
<p>加载数据</p>
</div>
<script src='./jquery.js'></script>
<script>
var pageIndex = 0;
var loading = false;
//开始就加载数据
getData()
$('.box').on('click', 'li', function (event) {
event.preventDefault();
})
//window滚动事件
$(window).on('scroll', function () {
//加载数据
getData();
})
//请求数据
function getData() {
if (!loading && isVisible($('.ct>p'))) {
$.get('/getNews', { page: pageIndex, len: 5 }).done(function (ret) {
handleSuccess(ret)
}).fail(function (error) {
loading = false;
})
loading = true;
}
}
//处理成功的回调
function handleSuccess(ret) {
//拼接显示
if (ret.length > 0) {
handleData(ret)
} else {
$('.box').append('<li><a href="#">' + '没有更多数据了。。。' + '</a></li>');
}
}
//处理返回数据不为0条
function handleData(ret) {
$.each(ret, function () {
$('.box').append('<li><a href="#">' + this + '</a></li>');
});
pageIndex++;
loading = false;
//继续加载数据
getData();
}
//判断p(加载数据)是否一出现在可视范围
function isVisible($node) {
var windowHeight = $(window).outerHeight(true);
var scrollTop = $(window).scrollTop();
var nodeHeight = $node.outerHeight(true);
var offsetHeight = $node.offset().top;
if ((scrollTop > offsetHeight - windowHeight) && (scrollTop < offsetHeight + nodeHeight)) {
return true;
} else {
return false;
}
}
</script>
</body>
</html>