Is there a way to change an image’s src ‘attribute’ into data_src within Hype?
So instead of: <img src="cheese.jpg" />
it looks like <img class="lazy" data-src="cheese.jpg" />
when exported from Hype?
I’m trying to see if I can “lazy load” images when exported from Hype but for that I need to change some image attributes to prevent all images to “lazy load” (if I get it to work at all).
You can have images as inner HTML if you start with a blank element (Rectangle) and then reference the image via the ${resourcesFolderName} like:
<img src="${resourcesFolderName}/myImage.png">
The ${resourcesFolderName} magic variable is replaced at export time with the name of your .hyperesources folder so it gets referenced correctly.
Also make sure to uncheck Automatically optimize when exporting for the image in the resources library otherwise there’s a chance Hype may be changing the name as part of the optimization process.
I found a very small script that smoothly lazy loads images. It looks if an image has a “src” or an HTML5 “data-src” attribute. The script leaves the images with a sole “src” attribute alone but lazy loads images with the “data-src” attribute.
The problem is that Hype doesn’t seem to accept a “data-src” attribute.
<img src="${resourcesFolderName}/myImage.png">
is ok but <img data-src="${resourcesFolderName}/myImage.png">
is not ok.
That is the little ‘bump in the road’ I’m stumbling over.
@h_classen Thank you so very much! I tried to put the script on sceneload but removing the DomContentLoaded is something I never would consider, given my lack of knowledge when it comes to scripts and code.
Thank you again for your help.
@MaxZieb Goal is to keep it simple To load when the image comes into view.
data-src is not a specific attribute that does anything and has no meaning to browsers. The external javascript library you are using does make use of it, though. Often external libraries scan the DOM (HTML structure of the page) and then do operations; since the DOM is dynamically constructed by Hype's runtime, the library's scan can sometimes have happened before Hype made the elements and therefore it didn't do anything. The solution is to manually invoke the scan at some point.
There is a good request to allow adding arbitrary HTML tag attributes (with or without the data- namespace) that is on our feature tracker .