...
|
...
|
@@ -114,10 +114,6 @@ export default { |
|
|
},
|
|
|
max() {
|
|
|
return this.clientWidth / 375 * this.loadDistance;
|
|
|
},
|
|
|
nowPage() {
|
|
|
this.nextPage
|
|
|
return this.page;
|
|
|
}
|
|
|
},
|
|
|
watch: {
|
...
|
...
|
@@ -125,51 +121,53 @@ export default { |
|
|
this.$nextTick(()=>{
|
|
|
setTimeout(()=>{
|
|
|
this.init();
|
|
|
},300);
|
|
|
}, 300);
|
|
|
});
|
|
|
},
|
|
|
data(newVal,oldVal) {
|
|
|
this.$nextTick(() => {
|
|
|
clearTimeout(this.timer);
|
|
|
this.timer = setTimeout(() => {
|
|
|
if(this.isresizing) {
|
|
|
if (this.isresizing) {
|
|
|
return;
|
|
|
}
|
|
|
if (newVal.length<this.loadedIndex) {
|
|
|
if (newVal.length < this.loadedIndex) {
|
|
|
this.loadedIndex = 0
|
|
|
}
|
|
|
if (newVal.length>oldVal.length || newVal.length>this.loadedIndex) {
|
|
|
if (newVal.length>oldVal.length || newVal.length > this.loadedIndex) {
|
|
|
if (newVal.length === oldVal.length) {
|
|
|
this.resize(this.loadedIndex>0?this.loadedIndex:null)
|
|
|
this.resize(this.loadedIndex > 0 ? this.loadedIndex : null);
|
|
|
return;
|
|
|
}
|
|
|
this.resize(oldVal.length>0?oldVal.length:null)
|
|
|
this.resize(oldVal.length > 0 ? oldVal.length : null)
|
|
|
}
|
|
|
},300);
|
|
|
}, 300);
|
|
|
});
|
|
|
},
|
|
|
page(newVal, oldVal) {
|
|
|
console.log('page=', newVal, oldVal);
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
init() { // initialize
|
|
|
init() { // initialize
|
|
|
this.root = this.$refs.vueWaterfall;
|
|
|
this.clearColumn();
|
|
|
var col = parseInt(this.col, 10);
|
|
|
for(var i = 0; i < col; i++) {
|
|
|
for (var i = 0; i < col; i++) {
|
|
|
let odiv = document.createElement('div');
|
|
|
|
|
|
odiv.className = 'vue-waterfall-column';
|
|
|
if (this.width) {
|
|
|
odiv.style.width = this.width + 'px'
|
|
|
odiv.style.width = this.width + 'px';
|
|
|
if (i !== 0) {
|
|
|
odiv.style.marginLeft = this.gutterWidth + 'px';
|
|
|
}
|
|
|
this.columnWidth = this.width
|
|
|
}
|
|
|
else {
|
|
|
this.columnWidth = this.width;
|
|
|
} else {
|
|
|
odiv.style.width = 100 / parseInt(col, 10) + '%';
|
|
|
this.columnWidth = 100 / parseInt(col, 10) / 100 * document.documentElement.clientWidth;
|
|
|
}
|
|
|
if(!this.root) {
|
|
|
if (!this.root) {
|
|
|
this.root = this.$refs.vueWaterfall;
|
|
|
}
|
|
|
this.root && this.root.appendChild(odiv);
|
...
|
...
|
@@ -181,184 +179,153 @@ export default { |
|
|
if (!dom.getElementByTagName) {
|
|
|
return;
|
|
|
}
|
|
|
var imgs = dom.getElementByTagName('img');
|
|
|
for(var i=0;i<imgs.length;i++)
|
|
|
{
|
|
|
var lazySrc = imgs[i].getAttribute('lazy-src');
|
|
|
if(!imgs[i].getAttribute('src') && lazySrc)
|
|
|
{
|
|
|
var newImg = new Image();
|
|
|
newImg.src = lazySrc;
|
|
|
if (newImg.complete)
|
|
|
{
|
|
|
var trueWidth = imgs[i].offsetWidth || this.columnWidth
|
|
|
var imgColumnHeight = newImg.height*trueWidth/newImg.width
|
|
|
|
|
|
if(imgs[i].offsetWidth)
|
|
|
{
|
|
|
imgs[i].style.height = imgColumnHeight+'px'
|
|
|
}
|
|
|
}
|
|
|
else{
|
|
|
await new Promise((resolve,reject)=>{
|
|
|
newImg.onload = function(){
|
|
|
var trueWidth = imgs[i].offsetWidth || this.columnWidth;
|
|
|
var imgColumnHeight = newImg.height * trueWidth / newImg.width;
|
|
|
|
|
|
if(imgs[i].offsetWidth)
|
|
|
{
|
|
|
imgs[i].style.height = imgColumnHeight+'px'
|
|
|
}
|
|
|
resolve()
|
|
|
}
|
|
|
newImg.onerror= function(){
|
|
|
resolve()
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
async append(dom){ //append dom element
|
|
|
|
|
|
var self = this
|
|
|
if(this.columns.length>0)
|
|
|
{
|
|
|
let min = this.columns[0]
|
|
|
for(var i = 1; i < this.columns.length; i++) {
|
|
|
if(await self.getHeight(min) > await self.getHeight(self.columns[i]) + 50){
|
|
|
min = self.columns[i];
|
|
|
}
|
|
|
}
|
|
|
min && min.appendChild(dom)
|
|
|
await this.setDomImageHeight(dom)
|
|
|
}
|
|
|
},
|
|
|
checkImg(dom){ //check has image
|
|
|
if(!dom)
|
|
|
{
|
|
|
return false
|
|
|
}
|
|
|
if(dom.getElementsByTagName&&dom.getElementsByTagName("img").length)
|
|
|
{
|
|
|
return true
|
|
|
}
|
|
|
else{
|
|
|
return false
|
|
|
}
|
|
|
},
|
|
|
async resize(index,elements){ //resize and render
|
|
|
this.isresizing = true
|
|
|
var self = this
|
|
|
if(!this.$slots.default){
|
|
|
this.isresizing = false
|
|
|
return
|
|
|
}
|
|
|
if(!index&&index!=0&&!elements)
|
|
|
{
|
|
|
elements = this.$slots.default
|
|
|
this.loadedIndex = 0
|
|
|
this.clear()
|
|
|
}
|
|
|
else if(!elements){
|
|
|
this.loadedIndex = index
|
|
|
elements = this.$slots.default.splice(index)
|
|
|
}
|
|
|
var imgs = dom.getElementByTagName('img');
|
|
|
for (var i = 0; i < imgs.length; i++) {
|
|
|
var lazySrc = imgs[i].getAttribute('lazy-src');
|
|
|
if (!imgs[i].getAttribute('src') && lazySrc) {
|
|
|
var newImg = new Image();
|
|
|
newImg.src = lazySrc;
|
|
|
if (newImg.complete) {
|
|
|
var trueWidth = imgs[i].offsetWidth || this.columnWidth
|
|
|
var imgColumnHeight = newImg.height*trueWidth/newImg.width
|
|
|
|
|
|
for(var j=0;j<elements.length;j++){
|
|
|
if(elements[j].elm&&self.checkImg(elements[j].elm))
|
|
|
{
|
|
|
var imgs = elements[j].elm.getElementsByTagName('img')
|
|
|
var newImg = new Image()
|
|
|
newImg.src = imgs[0].src
|
|
|
if(newImg.complete)
|
|
|
{
|
|
|
await self.append(elements[j].elm)
|
|
|
self.lazyLoad(imgs)
|
|
|
}
|
|
|
else{
|
|
|
await new Promise( (resolve,reject)=>{
|
|
|
newImg.onload = async function(){
|
|
|
await self.append(elements[j].elm)
|
|
|
self.lazyLoad(imgs)
|
|
|
resolve()
|
|
|
}
|
|
|
newImg.onerror= async function(){
|
|
|
await self.append(elements[j].elm)
|
|
|
self.lazyLoad(imgs)
|
|
|
resolve()
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
}
|
|
|
else{
|
|
|
await self.append(elements[j].elm)
|
|
|
}
|
|
|
self.loadedIndex++
|
|
|
}
|
|
|
this.isresizing = false
|
|
|
self.$emit('finish')
|
|
|
|
|
|
},
|
|
|
computedPx(img,imgApi){
|
|
|
if(imgs[i].offsetWidth) {
|
|
|
imgs[i].style.height = imgColumnHeight+'px'
|
|
|
}
|
|
|
} else{
|
|
|
await new Promise((resolve, reject) => {
|
|
|
newImg.onload = function() {
|
|
|
var trueWidth = imgs[i].offsetWidth || this.columnWidth;
|
|
|
var imgColumnHeight = newImg.height * trueWidth / newImg.width;
|
|
|
|
|
|
img.style.width = imgApi.width/this.columnWidth
|
|
|
},
|
|
|
lazyLoad(imgs){
|
|
|
if(!imgs)
|
|
|
{
|
|
|
if(!this.root)
|
|
|
{
|
|
|
this.root = this.$refs.vueWaterfall
|
|
|
}
|
|
|
imgs = this.root&&this.root.getElementsByTagName('img')
|
|
|
}
|
|
|
if(imgs[i].offsetWidth) {
|
|
|
imgs[i].style.height = imgColumnHeight+'px'
|
|
|
}
|
|
|
resolve();
|
|
|
};
|
|
|
newImg.onerror= function() {
|
|
|
resolve();
|
|
|
};
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
async append(dom) { // append dom element
|
|
|
var self = this;
|
|
|
|
|
|
if (this.columns.length > 0) {
|
|
|
let min = this.columns[0];
|
|
|
|
|
|
for (var i = 1; i < this.columns.length; i++) {
|
|
|
if (await self.getHeight(min) > await self.getHeight(self.columns[i]) + 50){
|
|
|
min = self.columns[i];
|
|
|
}
|
|
|
}
|
|
|
min && min.appendChild(dom)
|
|
|
await this.setDomImageHeight(dom)
|
|
|
}
|
|
|
},
|
|
|
checkImg(dom){ // check has image
|
|
|
if (!dom) {
|
|
|
return false;
|
|
|
}
|
|
|
if (dom.getElementsByTagName && dom.getElementsByTagName('img').length) {
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
},
|
|
|
async resize(index, elements) { // resize and render
|
|
|
this.isresizing = true;
|
|
|
var self = this;
|
|
|
if (!this.$slots.default) {
|
|
|
this.isresizing = false;
|
|
|
return;
|
|
|
}
|
|
|
if (!index && index != 0 && !elements) {
|
|
|
elements = this.$slots.default;
|
|
|
this.loadedIndex = 0;
|
|
|
this.clear();
|
|
|
} else if (!elements) {
|
|
|
this.loadedIndex = index;
|
|
|
elements = this.$slots.default.splice(index);
|
|
|
}
|
|
|
|
|
|
if(!imgs || imgs.length<0)
|
|
|
{
|
|
|
return
|
|
|
}
|
|
|
for(var index=0;index<imgs.length; index++)
|
|
|
{
|
|
|
if(imgs[index].className.match('animation')&&imgs[index].getAttribute('src'))
|
|
|
{
|
|
|
continue
|
|
|
}
|
|
|
else if(imgs[index].className.match('animation')&&!imgs[index].getAttribute('src'))
|
|
|
{
|
|
|
imgs[index].src = imgs[index].getAttribute('lazy-src')
|
|
|
imgs[index].removeAttribute('lazy-src')
|
|
|
}
|
|
|
else if(imgs[index].getAttribute('src')&&!imgs[index].className.match('animation')){
|
|
|
imgs[index].className = imgs[index].className + ' animation'
|
|
|
}
|
|
|
else if(!imgs[index].getAttribute('src')&&imgs[index].getBoundingClientRect().top<this.clientHeight+this.trueLazyDistance)
|
|
|
{
|
|
|
imgs[index].src = imgs[index].getAttribute('lazy-src')
|
|
|
imgs[index].className = imgs[index].className + ' animation'
|
|
|
imgs[index].removeAttribute('lazy-src')
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
clearColumn(){
|
|
|
this.columns.forEach((item)=>{
|
|
|
item.remove()
|
|
|
})
|
|
|
this.columns=[]
|
|
|
},
|
|
|
clear(){
|
|
|
this.columns.forEach((item)=>{
|
|
|
item.innerHTML = ''
|
|
|
})
|
|
|
},
|
|
|
mix(){
|
|
|
var elements = this.$slots.default
|
|
|
elements.sort(()=>{return Math.random()-0.5})
|
|
|
this.resize(0,elements)
|
|
|
},
|
|
|
for (var j = 0; j < elements.length; j++) {
|
|
|
if (elements[j].elm && self.checkImg(elements[j].elm)) {
|
|
|
var imgs = elements[j].elm.getElementsByTagName('img');
|
|
|
var newImg = new Image();
|
|
|
newImg.src = imgs[0].src;
|
|
|
if (newImg.complete) {
|
|
|
await self.append(elements[j].elm);
|
|
|
self.lazyLoad(imgs);
|
|
|
} else {
|
|
|
await new Promise((resolve, reject) => {
|
|
|
newImg.onload = async function() {
|
|
|
await self.append(elements[j].elm);
|
|
|
self.lazyLoad(imgs);
|
|
|
resolve();
|
|
|
};
|
|
|
newImg.onerror = async function() {
|
|
|
await self.append(elements[j].elm);
|
|
|
self.lazyLoad(imgs);
|
|
|
resolve();
|
|
|
};
|
|
|
});
|
|
|
}
|
|
|
} else {
|
|
|
await self.append(elements[j].elm)
|
|
|
}
|
|
|
self.loadedIndex++;
|
|
|
}
|
|
|
this.isresizing = false;
|
|
|
self.$emit('finish');
|
|
|
},
|
|
|
computedPx(img, imgApi) {
|
|
|
img.style.width = imgApi.width/this.columnWidth
|
|
|
},
|
|
|
lazyLoad(imgs) {
|
|
|
if (!imgs) {
|
|
|
if (!this.root) {
|
|
|
this.root = this.$refs.vueWaterfall
|
|
|
}
|
|
|
imgs = this.root&&this.root.getElementsByTagName('img')
|
|
|
}
|
|
|
if (!imgs || imgs.length < 0) {
|
|
|
return;
|
|
|
}
|
|
|
for (var index = 0; index < imgs.length; index++) {
|
|
|
if (imgs[index].className.match('animation') && imgs[index].getAttribute('src')) {
|
|
|
continue;
|
|
|
} else if (imgs[index].className.match('animation') && !imgs[index].getAttribute('src')) {
|
|
|
imgs[index].src = imgs[index].getAttribute('lazy-src')
|
|
|
imgs[index].removeAttribute('lazy-src')
|
|
|
} else if (imgs[index].getAttribute('src') && !imgs[index].className.match('animation')) {
|
|
|
imgs[index].className = imgs[index].className + ' animation'
|
|
|
} else if (!imgs[index].getAttribute('src') && imgs[index].getBoundingClientRect().top < this.clientHeight + this.trueLazyDistance) {
|
|
|
imgs[index].src = imgs[index].getAttribute('lazy-src')
|
|
|
imgs[index].className = imgs[index].className + ' animation'
|
|
|
imgs[index].removeAttribute('lazy-src')
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
clearColumn() {
|
|
|
this.columns.forEach((item) => {
|
|
|
item.remove();
|
|
|
});
|
|
|
this.columns = [];
|
|
|
},
|
|
|
clear() {
|
|
|
this.columns.forEach((item) => {
|
|
|
item.innerHTML = '';
|
|
|
})
|
|
|
},
|
|
|
mix() {
|
|
|
var elements = this.$slots.default;
|
|
|
elements.sort(() => {return Math.random() - 0.5});
|
|
|
this.resize(0, elements);
|
|
|
},
|
|
|
async getHeight(dom) {
|
|
|
return dom.offsetHeight;
|
|
|
},
|
...
|
...
|
@@ -371,7 +338,7 @@ export default { |
|
|
const scrollHeight = this.height ? this.root.scrollHeight : this.root.scrollHeight; //document.documentElement.offsetHeight
|
|
|
var diff = scrollHeight - scrollTop - self.clientHeight;
|
|
|
|
|
|
console.log('scrollTop=', scrollTop, 'scrollHeight=', scrollHeight, 'diff=', diff, 'max=', self.max);
|
|
|
// console.log('scrollTop=', scrollTop, 'scrollHeight=', scrollHeight, 'diff=', diff, 'max=', self.max);
|
|
|
|
|
|
self.$emit('scroll', {scrollHeight: scrollHeight, scrollTop: scrollTop, clientHeight: self.clientHeight, diff: diff, time: Date.now()})
|
|
|
if (diff < self.max && self.loadmore && scrollHeight > self.clientHeight) {
|
...
|
...
|
|