I have cloned the OOTB displaytemplate "Item_PictureOnTop.html". I would like to show a default picture if the picture not found on the item. Is that possible with custom css?
In your copy of Item_PictureOnTop.html you have to add some additional javascript logic to check if there is an image, and if not, insert something else.
An example could be
<!--#_
// ...
var noPictureFoundImgUrl = "url to image for no picture found"
var imageUrl = null;
if ($isEmptyString(ctx.CurrentItem.ImageUrlColumn))
{
imageUrl = noPictureFoundImgUrl;
}
else
{
imageUrl = ctx.CurrentItem.ImageUrlColumn;
}
// then insert the 'imageUrl' variable in the markup
// ...
_#-->
Daniels solution checks for empty url, not if an image was not loaded.
I don't think you can do it with CSS since onload of an image is a JavaScript event.
There is no notloaded event
It requires some (minor) changes to the DisplayTemplate to do the opposite:
Reveal loaded images
- add an onload event to the image
- display your default IMG in a DIV with absolute position on top of the image
- in the onload that fires (for every image) remove the default IMG that is covering this image
That way you can also add fancy CSS animations
Als je er niet uitkomt, kun je me inhuren...
CSS only (by Jussi Palo)
You can achieve something with CSS only if you add:
.cbs-pictureOnTopImageContainer{
background-image:url('/mydefaultimage.jpg');
background-size:cover;
}
But the broken image indicator will be displayed
-
I stand corrected, you can also try the JavaScript error event on an image: stackoverflow.com/questions/1977871/… – Danny '365CSI' Engelman Sep 21 '15 at 12:53