javascript - tippy.js 工具提示中的延迟加载图像?

标签 javascript jquery html

我正在使用 tippy.js 生成工具提示。一切正常,但是我有很多工具提示,每个工具提示都加载了不同的图像内容。为了减少加载时间,我想延迟加载图像以仅在工具提示可见时显示。

我知道 tippy.js 有一个内置函数来调用 AJAX 内容 (https://atomiks.github.io/tippyjs/ajax/) - 但是我想加载的内容在 HTML 中,我不想为每个工具提示编写单独的 javascript。理想情况下,以某种方式获取我悬停的特定提示的 img scr 并调用它以加载 onShow(instance) ~ 即 fetch('$(this).find("img").data("源代码");')。我尝试了一些变体,但没有任何效果。

这是一个代码笔(没有)延迟加载图像:https://codepen.io/jinch/pen/aboNOvP?editors=1010

我试图操纵他们网站上的 AJAX 示例来工作,但运气不好(见下文)。

tippy('body .tippy', {
            theme: 'google',
            touchHold: true,
            hideOnClick: false,
            interactive: true,
            placement: 'left',
            distance: 20,
            arrow: true,
            animateFill: false,
            animation: 'shift-away',

              onShow(instance) {
                if (instance.state.ajax === undefined) {
                  instance.state.ajax = {
                    isFetching: false,
                    canFetch: true,
                  }
                }
                if (instance.state.ajax.isFetching || !instance.state.ajax.canFetch) {
                  return
                }

                fetch('$(this).find("img").data("src")')
                  .then(response => response.blob())
                  .then(blob => {
                  })
                  .catch(error => {
                    // ...
                  })
                  .finally(() => {
                    instance.state.ajax.isFetching = false
                  })
              },
              onHidden(instance) {
                instance.setContent('Loading...')
                instance.state.ajax.canFetch = true
              },
          })

想法结果是只在调用时加载图像以减少页面的初始加载时间。

最佳答案

感谢@atomiks,您可以在这里找到答案:https://github.com/atomiks/tippyjs/issues/562#issuecomment-521650755

关于javascript - tippy.js 工具提示中的延迟加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500003/

相关文章:

javascript forEach数组回调时间?

javascript - jQuery : dynamic events how to achieve

javascript - 如何在选中复选框时获取不同表中的表行数据?

html - SCSS - 从一组样式中排除指定的容器

html - 打印 : Meta for Print page

javascript - 为什么 Controller 函数不执行 Angular Js 1.5.5

javascript - 使用正则表达式增加字符串中的数字

javascript - 如何在不重新加载页面的情况下更改 BrowserWindow 哈希 url

jquery - 在事件上调用处理函数

jquery - 使用 JQuery 动态更改图像链接