1.在主题buyu.style.css文件添加如下代码
/*给图片过渡效果*/
img{transition: all 0.5s;-moz-transition: all 0.5s; /* Firefox 4 */-webkit-transition: all 0.5s; /* Safari 和 Chrome */-o-transition: all 0.5s; /* Opera */;}
.index{width: 400px;margin: 0 auto;padding-bottom: 100px;}
.index-img{width: 100%;height:210px;margin-top:100px;}
/*object-fit: cover;解决图片指定大小被压缩问题*/
.index-img>img{width: 100%;height: 100%;object-fit: cover;}
2.在主题buyu.style.js文件内最后一行添加如下代码
//监听滚动条事件
window.onscroll = function(){
Limg()
}
//页面加载时调用一次,使图片显示
window.onload = function() {
var img = document.querySelectorAll("img[data-src]")
for(var i = 0; i < img.length; i++) {
img[i].style.opacity = "0"
}
Limg()
}
function Limg() {
var viewHeight = document.documentElement.clientHeight // 可视区域的高度
var t = document.documentElement.scrollTop || document.body.scrollTop;
var limg = document.querySelectorAll("img[data-src]")
// Array.prototype.forEach.call()是一种快速的方法访问forEach,并将空数组的this换成想要遍历的list
Array.prototype.forEach.call(limg, function(item, index) {
var rect
if(item.getAttribute("data-src") === "")
return
//getBoundingClientRect用于获取某个元素相对于视窗的位置集合。集合中有top, right, bottom, left等属性。
rect = item.getBoundingClientRect()
// 图片一进入可视区,动态加载
if(rect.bottom >= 0 && rect.top < viewHeight) {
(function() {
var img = new Image()
img.src = item.getAttribute("data-src")
item.src = img.src
//给图片添加过渡效果,让图片显示
var j = 0
setInterval(function() {
j += 0.2
if(j <= 1) {
item.style.opacity = j
return
}
}, 100)
item.removeAttribute('data-src')
})()
}
})
}
3.最后在主题page.php/post.php页面找到如下代码
<?php $this->content(); ?>
改成
<?php
$pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
$replacement = '<img class="index-img" data-src="$1" alt="'.$this->title.'">';
$content = preg_replace($pattern, $replacement, $this->content);
echo $content;
?>
图片灯箱功能可以去扒我主题代码
buyu_1.2.2.zip
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)