[英]Mobile first design, loading images and jquery DOM selection
I am attempting a mobile first approach to site design. 我正在尝试一种移动优先的网站设计方法。 As part of this, I am using a media query to detect screen width over 768px 为此,我正在使用媒体查询来检测768px以上的屏幕宽度
@media only screen and (min-width: 768px)
This triggers an image to be loaded through the CSS 这触发图像通过CSS加载
h1:before{content: url(../static/logo.png)" ";}
Now, when this image loads, I would like to make it a hyperlink as well. 现在,当加载此图像时,我也想使其成为超链接。 As I am already using jquery elsewhere, I think the easiest thing to do would be to select the image in the DOM and add an hyperlink element to the object. 正如我已经在其他地方使用jquery一样,我认为最简单的方法是在DOM中选择图像并将超链接元素添加到对象。 However, it looks to me like the image does not appear in the DOM because I use CSS to load the image (I'm unsure if the fact it is a pseudo-element makes a difference). 但是,在我看来,该图像似乎没有出现在DOM中,因为我使用CSS来加载该图像(我不确定是否为伪元素这一事实有所作为)。
What is an effective strategy for loading objects (images) and applying markup to them in a mobile-first strategy? 在移动优先策略中加载对象(图像)并将标记应用于对象的有效策略是什么? Thanks! 谢谢!
Just use jQuery to insert the image and the link into the DOM. 只需使用jQuery将图像和链接插入DOM。
if ($(window).width() > 768) {
$('h1').before('<a href="#"><img src="images/logo.jpg" /></a>');
}
You cannot select or manipulate pseudo elements added with :before
or :after
. 您不能选择或操纵添加了:before
或:after
伪元素。 They simply don't appear in the DOM. 它们根本不会出现在DOM中。 From the Generated content spec : 根据生成的内容规范 :
Generated content does not alter the document tree. 生成的内容不会更改文档树。 In particular, it is not fed back to the document language processor (eg, for reparsing). 特别是,它不会反馈给文档语言处理器(例如,用于重新解析)。
See this example at jsFiddle . 请参阅jsFiddle上的示例 。
Using jQuery to add the image and the link -- like in Rusty Jeans' answer -- is one good way to go. 使用jQuery添加图像和链接-就像Rusty Jeans的答案一样-是一种不错的方法。
If you are concerned about resizes, The most that the CSS approach would buy you is that the image would show/hide even if JS was disabled. 如果您担心调整大小,那么CSS方法可以为您带来的最大好处就是,即使禁用了JS,图像也将显示/隐藏。 In order to "linkify" it, you will need to monitor the resize
event, irregardless. 为了“链接”它,您将需要监视resize
事件,无论如何。 (If resizing is really such a big issue for some reason.) (如果由于某种原因调整大小确实是一个大问题。)
If it was me, I'd use real HTML -- an image, prelinked -- instead of pseudo elements. 如果是我,我将使用真实的HTML(预先链接的图像)代替伪元素。 Use the media-query driven CSS to show or hide these nodes entirely ( display:none
, etc.). 使用媒体查询驱动的CSS可以完全显示或隐藏这些节点( display:none
等)。
Another approach is to add the image via CSS, so that JS-disabled browsers would work after a fashion. 另一种方法是通过CSS添加图像,以便禁用JS的浏览器可以正常工作。 BUT , then use JS to remove that CSS, pseudo element and to add a real element, with link, instead. 但是 ,然后使用JS删除该CSS(伪元素)并添加带有链接的真实元素。 That is, the "progressive-enhancement" approach. 也就是说,“渐进增强”方法。
:before
and :after
content is for presentational purposes, not structural, functional elements. :before
和:after
内容仅出于演示目的,而非结构性功能元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.