获取HTML的数据加载

布赖恩·布伊

因此,我正在制作一个简单的神奇宝贝应用程序,其中前端进行提取调用以从PokeAPI捕获神奇宝贝并显示它,但是在加载神奇宝贝时,您可以看到获取信息的加载速率不同。例如,口袋妖怪先加载,然后名称加载,然后背景加载,然后类型加载。反正有它吗,所以HTML一次填满了吗?

这是我的Javascript提取函数的外观,此外,这里还有该应用程序,因此您可以看到信息加载缓慢的方式/不是一次全部加载:https : //bui-pokemon.herokuapp.com/

pokeButton.addEventListener('click', (e)=> {
e.preventDefault();

const pokeNameSearch = pokeSearch.value.toLowerCase();

fetch(`https://pokeapi.co/api/v2/pokemon/${pokeNameSearch}`)
.then((response) => response.json())
.then(data => {
    if(pokeCard.classList.value === 'poke_card'){
        pokeCard.classList.add('border');
    };

    //If a pokemon was normal / flying type then flying should be shown in the background isntead of normal
    //because flying type is more of a defining characteristic of that pokemon rather than normal
    if(data.types.length > 1 && data.types[0].type.name === "normal" && data.types[1].type.name === "flying"){
        pokeCard.className = `poke_card border ${data.types[1].type.name}_background`
    } else {
        pokeCard.className = `poke_card border ${data.types[0].type.name}_background`;
    }
    
    pokeImg.src = data.sprites.front_default;
    pokeName.innerHTML = data.name;

    // Fill in Pokemon Type
    pokeTypeIcon1.src = "";
    pokeTypeIcon2.src = "";
    pokeTypeIcon1.className = '';
    pokeTypeIcon2.className = '';
    pokeTypeIcon2.style.display = "none";

    pokeType.innerHTML = `${data.types[0].type.name}`;
    pokeNumDisplay.innerHTML = `#${fillInZero(data.id.toString())}`
    pokeTypeIcon1.src = `img/icons/${data.types[0].type.name}.svg`
    pokeTypeIcon1.className = `type_icon_1 ${data.types[0].type.name}`
    
    if(data.types.length > 1){
        pokeType.innerHTML += `/${data.types[1].type.name}`
        
        pokeTypeIcon2.src = `img/icons/${data.types[1].type.name}.svg`
        pokeTypeIcon2.style.display = "inline-block";
        pokeTypeIcon2.className = `type_icon_2 ${data.types[1].type.name}`
        
    } else {
        pokeType.innerHTML = `${data.types[0].type.name}`
    }
})
丹尼尔·奈特斯

似乎字体和图像加载会导致此问题。

您可以做的是load在每个图像上侦听事件并过渡不透明度。然后,使用document.fonts.ready并过渡颜色:

const container = document.querySelector(".container");
const background = document.getElementById("background")
const imgOne = document.getElementById("img-one")
const imgTwo = document.getElementById("img-two")

const fadeIn = (el) => {
  el.addEventListener("load", () => el.style.opacity = "1")
}

document.fonts.ready.then(() => container.style.color = "#fff")

fadeIn(background)
fadeIn(imgOne)
fadeIn(imgTwo)

background.src = "https://ak.picdn.net/shutterstock/videos/9589454/thumb/2.jpg"
imgOne.src = "https://toppng.com/public/uploads/thumbnail/ikachu-8-bits-8-bit-pokemon-grid-11563233054e4sqfqyl2l.png"
imgTwo.src = "https://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/lightning-icon.png"
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
body {
  background: #43464b;
  font-family: "Press Start 2P", Arial, Helvetica, sans-serif;
}

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  position: relative;
  margin: 0 auto;
  width: 200px;
  height: 300px;
  color: transparent;
  overflow: hidden;
  transition: color 0.25s;
}

.container>*:not(#background) {
  z-index: 1;
}

img {
  opacity: 0;
  transition: opacity 0.25s;
}

#background {
  position: absolute;
  top: 0;
  left: 0;
}

#img-one,
#img-two {
  height: 80px;
}
<div class="container">
  <img id="background" />
  <img id="img-one" />
  <p>#25 Pikachu</p>
  <img id="img-two" />
  <p>Electric</p>
</div>


但是,如果一件事要比另一件事花费更长的时间,这种方式可能仍然不平衡。

另一种解决方案是在加载后将值推送到数组,并在设置容器的不透明度之前检查数组的长度:

const container = document.querySelector(".container");
const background = document.getElementById("background")
const imgOne = document.getElementById("img-one")
const imgTwo = document.getElementById("img-two")

const loaded = []

const setLoadState = (el) => {
  loaded.push(el)

  if (loaded.length === 4)
    container.style.opacity = "1"
}
const imgLoad = (img) => {
  img.addEventListener("load", () => setLoadState(img.id))
}

document.fonts.ready.then(() => setLoadState('font'))
imgLoad(background)
imgLoad(imgOne)
imgLoad(imgTwo)

background.src = "https://ak.picdn.net/shutterstock/videos/9589454/thumb/2.jpg"
imgOne.src = "https://toppng.com/public/uploads/thumbnail/ikachu-8-bits-8-bit-pokemon-grid-11563233054e4sqfqyl2l.png"
imgTwo.src = "https://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/lightning-icon.png"
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
body {
  background: #43464b;
  font-family: "Press Start 2P", Arial, Helvetica, sans-serif;
}

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  position: relative;
  margin: 0 auto;
  width: 200px;
  height: 300px;
  color: #fff;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.25s;
}

.container>*:not(#background) {
  z-index: 1;
}

#background {
  position: absolute;
  top: 0;
  left: 0;
}

#img-one,
#img-two {
  height: 80px;
}
<div class="container">
  <img id="background" />
  <img id="img-one" />
  <p>#25 Pikachu</p>
  <img id="img-two" />
  <p>Electric</p>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何获取使用JavaFx WebView的html加载功能生成的数据?

来自分类Dev

jQuery:无法获取加载了JSON数据的HTML 5表的值

来自分类Dev

从AngularJS完成数据加载后获取html正文高度

来自分类Dev

从加载的WebView获取HTML代码

来自分类Dev

从加载的WebView获取HTML代码

来自分类Dev

从html框架获取数据

来自分类Dev

如何从HTML获取数据?

来自分类Dev

jQuery从ajax加载的数据获取元素ID

来自分类Dev

加载后获取Highcharts系列数据

来自分类Dev

在获取数据之前正在加载TableView

来自分类Dev

从“数据”标签获取文本并加载动画

来自分类Dev

Nextjs在重新加载页面时获取数据

来自分类Dev

jQuery加载获取数据,而不是插入元素

来自分类Dev

加载TableData之前如何从CloudKit获取数据

来自分类Dev

jQuery从加载的ajax获取元素ID数据

来自分类Dev

从服务获取数据时 HTTP 加载失败

来自分类Dev

在等待获取数据时显示加载屏幕

来自分类Dev

仅在获取数据时加载状态

来自分类Dev

主页加载时如何从Firestore获取数据?

来自分类Dev

加载HTML时获取Webview文档标题

来自分类Dev

加载html后如何获取容器高度

来自分类Dev

jQuery加载HTML后获取ID

来自分类Dev

页面加载后获取HTML信息

来自分类Dev

加载HTML时获取Webview文档标题

来自分类Dev

加载html后如何获取容器高度

来自分类Dev

加载后无法获取html内容

来自分类Dev

使用 PHP 获取加载的操纵 HTML dom

来自分类Dev

从html获取特定数据

来自分类Dev

jQuery不获取HTML数据