简体   繁体   English

延迟加载样式=“跨度标签内的背景图像”

[英]Lazy load style="background-image inside a span tag

I've implemented lazy load images to most of my website (USING THIS https://github.com/tuupola/lazyload ), however I have a shop page which is a load of thumbnails which are using the background-image tag, the code I have is as follows (it's running on smarty template system):-我已经在我的大部分网站上实现了延迟加载图像(使用此https://github.com/tuupola/lazyload ),但是我有一个商店页面,其中包含大量使用背景图像标签的缩略图,我拥有的代码如下(它在 smarty 模板系统上运行):-

{section name=c loop=$cats->mCats}
<div class="block"><a class="clearfix" href="{$smarty.const.SITE_ROOT}/{$cats->mCats[c].menulinktext}/"><span class="thumb" style="background-image: url({$smarty.const.SITE_ROOT}{$smarty.const.IMG_DIR}/thumbnails/{$cats->mCats[c].img_category});"></span><span class="info">{$cats->mCats[c].name|escape:'htmlall'|widont}</span></a></div>{/section}

The CSS:- CSS:-

.blocks1 .block a span.thumb {
width: 120px;
height: 90px;
background-repeat: no-repeat;
background-position: center;
background-size: contain;
margin: 0 auto

} }

All the examples I can find rely on the background-image part being inside a div but mine is inside a span tag.我能找到的所有示例都依赖于背景图像部分位于 div 内,但我的位于 span 标签内。

Is there a way to get this to work?有没有办法让它工作?

Reading online suggest span is used to style text but the original dev who wrote this seems to be using it for images?在线阅读建议 span 用于设置文本样式,但编写此内容的原始开发人员似乎将其用于图像?

Thanks for any help on this, I think I've tried just about everything to get this to work!!感谢您对此的任何帮助,我想我已经尝试了几乎所有方法来让它工作!

did you try to do it with a <Div> ?您是否尝试使用<Div>来做到这一点?

W3School suggests "The <span> tag is an inline container used to mark up a part of a text, or a part of a document." W3School建议“ <span>标签是一个内联容器,用于标记文本的一部分或文档的一部分。” And for Div: "The <div> tag defines a division or a section in an HTML document."对于 Div:“ <div>标签定义 HTML 文档中的一个部门或一个部分。” I think it shouldn't make a big difference, as long as it doesn't bother your others styles?我认为它应该不会有太大的不同,只要它不打扰你的其他人styles?

The <span> tag is per definition an inline element used to format text. <span>标签根据定义是一个用于格式化文本的内联元素。 You can however turn it into a block element, so it will behave exactly like a div.但是,您可以将其转换为元素,因此它的行为与 div 完全相同。

try adding this to your CSS rule:尝试将此添加到您的 CSS 规则中:

.blocks1 .block a span.thumb {
    display: block;
}

The concept is explained here: https://www.w3schools.com/html/html_blocks.asp这个概念在这里解释: https://www.w3schools.com/html/html_blocks.asp

If your items need to be displayed next to each other and you don't want them to wrap after each item, try display: inline-block;如果您的项目需要彼此相邻显示并且您不希望它们在每个项目之后换行,请尝试display: inline-block; instead.反而。

I got this working using a different plugin ( http://jquery.eisbehr.de/lazy/#about )我使用不同的插件( http://jquery.eisbehr.de/lazy/#about )得到了这个工作

I added this to the CSS:-我将此添加到 CSS:-

div.lazy {
    width: 120px;
    height: 90px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    margin: 0 auto;
    display: block;

} }

Changed the tpl code to: -将 tpl 代码更改为:-

{section name=c loop=$cats->mCats}
<div class="block"><a class="clearfix" href="{$smarty.const.SITE_ROOT}/{$cats->mCats[c].menulinktext}/"><div class="lazy" data-src="{$smarty.const.SITE_ROOT}{$smarty.const.IMG_DIR}/thumbnails/{$cats->mCats[c].img_category}" /></div><span class="info">{$cats->mCats[c].name|escape:'htmlall'|widont}</span></a></div>{/section}

And I now have fully lazy loading images:)我现在有完全延迟加载图像:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM