<template>
<div id="container" class="container">
<img
v-for="(item, index) in list"
:key="item.topicid + index"
:src="item.imgsrc"
alt=""
/>
</div>
</template>
<script lang="ts" setup>
import axios from "axios";
import { onMounted, ref, watch, nextTick } from "vue";
import { throttle } from 'lodash-es'
const list = ref<any>([]);
const getListData = () => {
axios
.get("https://c.m.163.com/nc/auto/list/6YOR5bee/0-100.html")
.then((res) => {
console.log(res.data.list, "xxxxxxxxxxxxxxxxxx");
list.value = res.data.list;
});
};
getListData();
function cal() {
const divContainer: any = document.getElementById("container");
const imgWidth = 220;
var containerWidth: any = divContainer?.clientWidth;
console.log(containerWidth, "containerWidth");
var columns = Math.floor(containerWidth / imgWidth);
console.log(columns, "columns");
var spaceNumber = columns + 1;
var leftSpace = containerWidth - columns * imgWidth;
var space = leftSpace / spaceNumber;
return {
space,
columns,
};
}
function setPoisions() {
const divContainer: any = document.getElementById("container");
const imgWidth = 220;
var info = cal();
var nextTops = new Array(info.columns);
nextTops.fill(0);
setTimeout(() => {
for (var i = 0; i < divContainer?.children.length; i++) {
var img = divContainer.children[i];
console.log(img,'img')
var minTop = Math.min.apply(null, nextTops);
console.log(minTop,'minTop')
img.style.top = minTop + "px";
var index = nextTops.indexOf(minTop);
nextTops[index] += img.height + info.space;
var left = (index + 1) * info.space + index * imgWidth;
img.style.left = left + "px";
}
console.log(nextTops);
var max = Math.max.apply(null, nextTops);
divContainer.style.height = max + "px";
}, 1000);
}
onMounted(() => {
setPoisions();
window.onresize =throttle(setPoisions,1000)
});
watch(
() => list.value,
(newV) => {
console.log(newV, "newV");
}
);
</script>
<style lang="scss" scoped>
.container {
width: 90%;
height: 100vh;
margin: 0 auto;
border: 2px solid;
position: relative;
img {
position: absolute;
width: 220px;
transition: 0.3s;
}
}
</style>
<template>
<div id="container" class="container RED">
<div v-for="(item, index) in list"
:key="item.topicid + index">
<img
:src="item.imgsrc"
alt=""
/>
序号 {{ index+1 }}
</div>
</div>
</template>
<script lang="ts" setup>
import axios from "axios";
import { onMounted, ref, watch, nextTick } from "vue";
import { throttle } from 'lodash-es'
const list = ref<any>([]);
const getListData = () => {
axios
.get("https://c.m.163.com/nc/auto/list/6YOR5bee/0-100.html")
.then((res) => {
console.log(res.data.list, "xxxxxxxxxxxxxxxxxx");
list.value = res.data.list;
});
};
getListData();
function cal() {
const divContainer: any = document.getElementById("container");
const imgWidth = 220;
var containerWidth: any = divContainer?.clientWidth;
console.log(containerWidth, "containerWidth");
var columns = Math.floor(containerWidth / imgWidth);
console.log(columns, "columns");
var spaceNumber = columns + 1;
var leftSpace = containerWidth - columns * imgWidth;
var space = leftSpace / spaceNumber;
return {
space,
columns,
};
}
function setPoisions() {
const divContainer: any = document.getElementById("container");
const imgWidth = 220;
var info = cal();
var nextTops = new Array(info.columns);
nextTops.fill(0);
setTimeout(() => {
for (var i = 0; i < divContainer?.children.length; i++) {
var div = divContainer.children[i]
console.log(div.clientHeight,'top',div.height)
var minTop = Math.min.apply(null, nextTops);
console.log(minTop,'minTop')
div.style.top = minTop + "px";
var index = nextTops.indexOf(minTop);
nextTops[index] += div.clientHeight + info.space;
var left = (index + 1) * info.space + index * imgWidth;
div.style.left = left + "px";
}
console.log(nextTops);
var max = Math.max.apply(null, nextTops);
divContainer.style.height = max + "px";
}, 1000);
}
onMounted(() => {
setPoisions();
window.onresize =throttle(setPoisions,1000)
});
watch(
() => list.value,
(newV) => {
console.log(newV, "newV");
}
);
</script>
<style lang="scss" scoped>
.container {
width: 90%;
height: 100vh;
margin: 0 auto;
border: 2px solid;
position: relative;
>div {
position: absolute;
width: 220px;
transition: 0.3s;
img{
width: 220px;
}
}
}
</style>