页面加载前随机重新订购DIV?
我一直在努力提高网站的Google页面速度。
除了最大的内容涂料外,这几乎是一个完美的分数。
我已经解决了这是因为我的页面顶部的轮播隐藏在页面加载上,然后我使用JavaScript随机将幻灯片随机洗牌:
shuffle(selector) {
let $elements = document.querySelectorAll(selector)
let array = []
$elements.forEach(($el) => {
array.push($el.cloneNode(true))
})
$elements.forEach(($el, i) => {
$el.replaceWith(array[i=Math.floor(Math.random()*array.length)])
array.splice(i,1)
})
},
init() {
const $carousel = document.querySelector('.carousel.shuffle'),
if ($carousel) {
this.shuffle('.carousel-card')
$carousel.style.opacity = 1
}
},
这会导致延迟,因为它必须等到DOM为止然后满载,然后运行我的脚本,因此显示了LCP(第一个幻灯片映像)的时间比其他脚本迟到。
是否有任何方法可以在页面加载之前将Div散装?
我的页面被缓存,因此我认为无法完成服务器端。
我以为我可以使用这个答案但是我不确定在CSS中是否可以随机随机吗?
I've been working on improving the Google Page Speed of my site.
It's nearly a perfect score except for Largest Contentful Paint.
I've worked out this is because my Carousel at the top of my page is hidden on page load, then I'm using javascript to shuffle the slides randomly before showing it:
shuffle(selector) {
let $elements = document.querySelectorAll(selector)
let array = []
$elements.forEach(($el) => {
array.push($el.cloneNode(true))
})
$elements.forEach(($el, i) => {
$el.replaceWith(array[i=Math.floor(Math.random()*array.length)])
array.splice(i,1)
})
},
init() {
const $carousel = document.querySelector('.carousel.shuffle'),
if ($carousel) {
this.shuffle('.carousel-card')
$carousel.style.opacity = 1
}
},
This causes a delay because it has to wait until the DOM is fully loaded then run my script and therefore shows the LCP (the first slide image) later than it would otherwise.
Is there any way to shuffle the div's before the page loads?
My page is cached so I don't think it can be done server-side.
I was thinking I could maybe use CSS like this answer but I'm not sure random is possible in CSS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 createocumentFragment api(而不是为每个元素进行更新)
类似:
you could try to speed up the dom update by using createDocumentFragment api (instead of having an update for each element move)
something like: