简体   繁体   English

用css替换css background-image:url(...) <img> 标记并继续滚动效果

[英]Replace css background-image: url(…) with <img> tag and keep scrolling over effect

I want to add alt tags to images on my website to improve SEO. 我想在我的网站上为图像添加alt标签以改善SEO。 The problem is I'm embedding them using CSS background-image: url(...) . 问题是我使用CSS background-image:url(...)嵌入它们。

It creates the desired scrolling effects (see below), but is not good for SEO. 它创建了所需的滚动效果(见下文),但对SEO不利。

Current code: 当前代码:

 .text { margin: 200px 20px; } .image-background { background-attachment: fixed; background-position: 50% 50%; background-repeat: no-repeat; background-size: 100%; display: block; height: 800px; margin-bottom: 150px; margin-left: -1500px; margin-right: -1500px; margin-top: 150px; width: 3500px; } .image1 { background-image: url(https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg); } .image2 { background-image: url(http://media1.santabanta.com/full1/Animals/Cats/cats-149a.jpg); } 
 <div class='text'> Lorem ipsum dolores... </div> <div class='image-background image1'></div> <div class='text'> Lorem ipsum dolores... </div> <div class='image-background image2'></div> <div class='text'> Lorem ipsum dolores... </div> 

The question is: how do I add <img> tags with alt properties without breaking the visual appearance? 问题是:如何在不破坏视觉外观的情况下添加带有alt属性的<img>标签?

Edit: 编辑:

I tried using <img> with css position:fixed but can't get it to work well with more than one image ( my broken jsfiddle here ). 我尝试使用带有css 位置的 <img> :已修复,但无法使其与多个图像一起使用我的破解jsfiddle )。

Edit 2: 编辑2:

These images are part of website content, not layout. 这些图像是网站内容的一部分, 而不是布局。 They deserve alt tags, I'm not trying to stuff more keywords in a "bad" way. 他们应该得到alt标签,我不是试图以“糟糕”的方式填充更多关键字。 I originally put them as backgrounds to achieve a visual effect. 我最初把它们作为背景来实现视觉效果。 Now I want to fix the mistake, but without changing how the website looks like. 现在我想解决这个错误,但不改变网站的样子。 I'm talking about my photos on this blog . 我正在谈论我在这个博客上的照片

Edit 3: 编辑3:

I'm not trying to use only CSS here. 我不是想在这里使用CSS。 Any code modification, JS library or pretty much anything is fine! 任何代码修改,JS库或几乎任何东西都没问题!

Method 1 方法1

This method doesn't change the visibility of the images, so I think there's no issues about SEO at all. 这种方法不会改变图像的可见性,所以我认为SEO根本没有问题。 But it is more complex and have the caveat that only one image can appear per once. 但它更复杂,并且需要注意的是每次只能出现一个图像。 So the text's div must to fill the entire screen resulting in a big padding. 所以文本的div必须填满整个屏幕,从而产生一个很大的填充。

 $(function(){ var texts = $('.text'); var oldY = 0; $(document).scroll(function(){ var y = window.scrollY; texts.each(function(){ var text = $(this); if(y >= text.offset().top) text.next().addClass('active'); else text.next().removeClass('active'); }); }); }); 
 body{ padding: 0; margin: 0; } .text{ padding: 20px; margin: 0 0 600px; position: relative; z-index: 3; background-color: #fff; min-height: 100vh; display: flex; align-items: center; } .background-img img{ position: fixed; top: 0; left: 0; z-index: 1; width: 100%; } .background-img.active img{ z-index: 2; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class='text'> Lorem ipsum dolores... </div> <div class="background-img active"> <img src="https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg" alt="Image 1"> </div> <div class='text'> Lorem ipsum dolores... </div> <div class="background-img"> <img src="http://media1.santabanta.com/full1/Animals/Cats/cats-149a.jpg" class="background-img" alt="Image 2"> </div> <div class='text'> Lorem ipsum dolores... </div> 

Method 2 方法2

This is simpler, and to say the truth it's almost the same idea from your original code. 这更简单,说实话,它与原始代码几乎完全相同。 The difference is that as soon as the page loads, the images are hidden and then copied as background images for their parent divs. 不同之处在于,一旦页面加载,图像就会被隐藏,然后被复制为父div的背景图像。 The advantage is that you can have more then one image visible at the same time, which is a better effect. 优点是你可以同时看到多个图像,这是一个更好的效果。

 $(function(){ $('.background-img img').each(function(){ var img = $(this).hide(); img.parent().css('background-image', 'url(' + img.prop('src') + ')'); }); }); 
 body{ padding: 0; margin: 0; } .text{ padding: 200px 20px; position: relative; z-index: 3; background-color: #fff; } .background-img{ height: 400px; background-attachment: fixed; background-repeat: no-repeat; background-position: center; background-size: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class='text'> Lorem ipsum dolores... </div> <div class="background-img"> <img src="https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg" alt="Image 1"> </div> <div class='text'> Lorem ipsum dolores... </div> <div class="background-img"> <img src="http://media1.santabanta.com/full1/Animals/Cats/cats-149a.jpg" class="background-img" alt="Image 2"> </div> <div class='text'> Lorem ipsum dolores... </div> 

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

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