Replace css background-image: url (...) with the <img> tag and continue to scroll through the effect

I want to add alt tags to images on my website to improve SEO. The problem is that I am embedding them using CSS background-image: url (...).

It creates the desired scroll effects (see below), but is not suitable for 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>
Run code

Question: how to add tags <img>with altproperties without disturbing the visual appearance?

Edit:

I tried using <img> with css position: fixed, but cannot make it work well with more than one image ( my broken jsfiddle here ).

Edit 2:

-, . -, "". . , , . .

3:

CSS . , JS !

+4
1

1

, , SEO. , . , 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>

2

, , , . , , , div. , , .

$(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>
+1

Source: https://habr.com/ru/post/1655722/


All Articles