【问题标题】:My html script has a LOT of href urls and is causing my page to slow down, how to solve this issue?我的 html 脚本有很多 href url,导致我的页面变慢,如何解决这个问题?
【发布时间】:2021-01-28 16:07:01
【问题描述】:

所以,我真的是脚本的初学者,我做了几个小时的研究以找到解决方案,但无法弄清楚。

我有一个“图像搜索 jquery 脚本。当我将所有内容都嵌入到一个 html 代码中时,它运行正常。然而,事情开始变得困难(浏览器速度慢、浏览器响应缓慢、计算机速度变慢......)图片链接数达到2000左右!

我认为可以解决问题的方法是将我的 html 代码拆分为单独的部分(html、js、css...)。

这就是我卡住的地方!

我需要您的帮助,了解如何将这些 href url 或链接保存在单独的文件中,然后从 html 中单独调用它们。

这是我所做的(再次强调,我不是专家):

index.html

<!DOCTYPE html>
<meta charset="UTF-8">
<head>    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
    <h2>Search for Image</h2>

    <input type="button" id="mybutton" value="Search!">
    <input type="text" id="myinput" name="search" placeholder="search..." style="width:50; height: 20px; border-radius: 4px; font-size: 18px;"><br><br>

    <a href="div_section.html"></a>
    <script src="javascripts/main.js"></script> 
</body>

ma​​in.js

$(document).ready(function() {
    $("#mybutton").on('click', function() {
        var mysrchbox = $("#myinput").val();
        mysrchbox = mysrchbox.replace(/[^a-zA-Z ]/g, "")
        var val = $.trim(mysrchbox);
        if (val === "") {
            $('#none').show();
            $('img').hide();
        } else {
            val = val.split(" ").join("\\ ");
            if ( $("img[alt*=" + val + " i]").attr('alt') === undefined ) {
                $('#none').show();
                $('img').hide();
            } else {
                $('#none').hide();
                $('img').hide();
                $("img[alt*=" + val + " i]").show();
                $("img[alt*=" + val + " i]").css('display', 'inline-flex');
            }
        }
    });
});

style.css

body {
    background: white !important;
}

#images {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    align-items: center;
    justify-content: center;
    flex-basis: 33.3%;
    width: 100%;
    margin: auto;
    text-align: center;
}

#images img {
    background: white;
    position: relative;
    margin: auto;
    display: none;
    object-fit: contain;
    height: max-content;
    padding: 0.5rem;
    text-align: center;
    margin: auto;
}

#images img:hover {
    opacity: 0.5;
}

div_section.html

<div id="images"><span id="none" hidden="true">no result related for your search.</span>
  <a href="#"><img src="C:\Users\user\dir-to-image\bitcoin-clipart-transparent-png.png" alt="eBitcoin" width=130></a>
  <a href="#"><img src="C:\Users\user\dir-to-image\cryptocurrency-wallet-ethereum-dogecoin-hd-png-download.png" alt="Ethereum" width=130></a>
  <a href="#"><img src="C:\Users\user\dir-to-image\/Ripple-Logo.png" alt="Ripple" width=130></a>
  <a href="#"><img src="C:\Users\user\dir-to-image\/tsmzy49d4adz.jpg" alt="eBitcoin Cash" width=130></a>
  <a href="#"><img src="C:\Users\user\dir-to-image\cardano-logo-png-5-Transparent-Images.jpg" alt="eCardano" width=130></a>
  <a href="#"><img src="C:\Users\user\dir-to-image\DJkf7M4VYAAgt8V.png" alt="NEM" width=130></a>
  <a href="#"><img src="C:\Users\user\dir-to-image\litecoin-logo.png" alt="LiteCoin" width=130></a>
  <a href="#"><img src="C:\Users\user\dir-to-image\1486429998.png" alt="Stellar Lumens" width=130></a>

</div>

【问题讨论】:

  • 这可能看起来很困难,我不知道您需要的所有细节,但我会考虑拥有一个图像名称和来源列表,然后在进行搜索后您可以选择动态加载使用 js 的热门搜索图片。这有意义吗?
  • 例如(在初始化时)var list = {"cat": "cat.png", "dog": "dog.jpeg" /*...etc*/} 和(搜索按钮)onclick = "search()",然后遍历列表条目以查找顶级匹配项,然后显示动态创建的顶级匹配源的 Image 对象
  • @OtherMe,感谢您的建议,但老实说,我不知道如何实施您的方法!
  • 欢迎您,下面的答案更简单,如果可行,我相信这将是一个更好的解决方案,所以一定要先尝试。
  • @OtherMe,我试过了,不幸的是它变慢了!它比我的原始代码更需要我的机器的内存。原始代码大约需要 1GB,建议代码需要超过 1.5GB。

标签: javascript html jquery


【解决方案1】:
  • loading="lazy"添加到所有&lt;img&gt;标签
  • 如果可能,请下载图像,使用在线工具对其进行压缩,将它们保存在您的项目文件夹中并更改src 属性以定位本地图像
  • 如果可能,删除 jQuery 并用以下代码替换您的代码

const button = document.querySelector('#mybutton')
const imgs = document.querySelectorAll('img')
const none = document.querySelector('#none')
imgs.forEach(img => img.style.display = 'none')

button.addEventListener('click', e => {
  const mysrchbox = document.querySelector('#myinput').value
  const val = mysrchbox.replace(/[^a-zA-Z ]/g, '').trim().toLowerCase()
  imgs.forEach(img => img.setAttribute('hidden', 'true'))
  if (val === '') {
    imgs.forEach(img => img.style.display = 'none')
    none.removeAttribute('hidden')
  } else {
    let showImgs = []
    imgs.forEach(img => {
      if (img.getAttribute('alt').toLowerCase().startsWith(val)) showImgs.push(img)
    })
    if (showImgs.length !== 0) {
      none.setAttribute('hidden', 'true')
      imgs.forEach(img => img.style.display = 'none')
      showImgs.forEach(img => img.style.display = 'inline-flex')
    } else {
      imgs.forEach(img => img.style.display = 'none')
      none.removeAttribute('hidden')
    }
  }
})
body {
  background: white !important;
}

#images {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  align-items: center;
  justify-content: center;
  flex-basis: 33.3%;
  width: 100%;
  margin: auto;
  text-align: center;
}

#images img {
background: white;
  position: relative;
  margin: auto;
  display: none;
  object-fit: contain;
  height: max-content;
  padding: 0.5rem;
  text-align: center;
  margin: auto;
}

#images img:hover {
  opacity: 0.5
}
<!DOCTYPE html>
<meta charset="UTF-8">

<head>
  <script src="javascripts/main.js" defer></script>
</head>

<body>
  <h2>Search for Image</h2>

  <input type="button" id="mybutton" value="Search!">
  <input type="text" id="myinput" name="search" placeholder="search..."
    style="width:50; height: 20px; border-radius: 4px; font-size: 18px;"><br><br>

  <div id="images"><span id="none" hidden="true">no result related for your search.</span>
    <a href="#"><img src="https://p.kindpng.com/picc/s/108-1082674_bitcoin-png-bitcoin-clipart-transparent-png.png" loading="lazy" alt="eBitcoin" width=130></a>
    <a href="#"><img src="https://png.pngitem.com/pimgs/s/692-6924771_blockchain-cryptocurrency-wallet-ethereum-dogecoin-hd-png-download.png" loading="lazy" alt="Ethereum" width=130></a>
    <a href="#"><img src="https://upload.wikimedia.org/wikipedia/commons/e/eb/Ripple-Logo.png" loading="lazy" alt="Ripple" width=130></a>
    <a href="#"><img src="https://i.redd.it/tsmzy49d4adz.jpg" loading="lazy" alt="eBitcoin Cash" width=130></a>
    <a href="#"><img src="https://freepikpsd.com/wp-content/uploads/2019/10/cardano-logo-png-5-Transparent-Images.jpg" loading="lazy" alt="eCardano" width=130></a>
    <a href="#"><img src="https://pbs.twimg.com/media/DJkf7M4VYAAgt8V.png" loading="lazy" alt="NEM" width=130></a>
    <a href="#"><img src="https://bitkee.com/icons/litecoin-logo.png" loading="lazy" alt="LiteCoin" width=130></a>
    <a href="#"><img src="http://bittrust.s3.amazonaws.com/1486429998.png" loading="lazy" alt="Stellar Lumens" width=130></a>
  
  </div>
</body>

【讨论】:

  • 对于这里的混乱,我深表歉意,我刚刚更正了我的图像路径/网址。事实上,我的 PC 上已经有我的图片本地了,但是因为它们大约有 2000 张图片,这意味着我将不得不使用 2000 行来创建链接/srcs。我在想是否有更好的方法将这些链接放在 html 之外的单独文件中。使用这些 URL 一起运行我的 HTML 文件会降低浏览器和我的 PC 的速度。
  • 并非如此,您必须使用 loading 属性或 Intersection Observer API 实现延迟加载到图像中。这将大大减少一次下载的数据量,并应提高性能。删除 jQuery 也能有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
  • 2023-01-24
  • 2019-04-19
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
相关资源
最近更新 更多