Answer the question
In order to leave comments, you need to log in
How to use jQuery to select src from images and substitute values into parent element's styles?
Greetings!
There is a slider with several items.
<div class="gallery__wrapper">
<!-- .gallery__item -->
<div class="gallery__item">
<div class="gallery__img-wrapper js-background">
<img src="img/gallery/gallery-7.jpg" alt="------------незаполенное описание------------" class="d-none js-image gallery__img">
</div>
<!-- gallery text -->
<div class="container">
<h3 class="gallery__title">ЗАГОЛОВОК 1 <br>заголовок</h3>
<p class="gallery__content">Далеко-далеко за словесными горами в стране, гласных и согласных живут рыбные тексты.</p>
<button class="d-block mx-auto accent-button">сделать на заказ</button>
</div>
</div>
<!-- .gallery__item -->
<div class="gallery__item">
<div class="gallery__img-wrapper js-background">
<img src="img/gallery/gallery-2.jpg" alt="------------незаполенное описание------------" class="d-none js-image gallery__img">
</div>
<!-- gallery text -->
<div class="container">
<h3 class="gallery__title">ЗАГОЛОВОК 2 <br>заголовок</h3>
<p class="gallery__content">Далеко-далеко за словесными горами в стране, гласных и согласных живут рыбные тексты.</p>
<button class="d-block mx-auto accent-button">сделать на заказ</button>
</div>
</div>
</div>
$(this).find(".gallery__item")(function(){
var bgSrc = $('.gallery__item > .js-background > .js-image').attr('src');
$('.gallery__item > .js-background').css('background-image', 'url("' + bgSrc + '")');
});
Answer the question
In order to leave comments, you need to log in
Understood. A few "crutches" and the implementation is not as intended, but still works:
<div class="gallery__item">
<div class="gallery__img-wrapper js-background"><-- удаляем в исходнике эту обёртку, генерируем ее с помощью скрипта-->
<img src="img/gallery/gallery-7.jpg" alt="------------незаполенное описание------------" class="d-none js-image gallery__img">
</div>
</div>
$(".js-image").each(function(){
var obj = $(this).attr("src");
$(this).wrap("<div class='gallery__img-wrapper js-background' style='background:url(" + obj + ");'></div>")
})
$('.gallery_item').click(function(){
var bgSrc = $('.gallery__item > .js-background > .js-image').attr('src');
$('.gallery__item > .js-background').css('background-image', 'url("' + bgSrc + '")');
});
$('.gallery_item').click(function(){
var bgSrc = $(this).find('.js-image').attr('src');
$(this).find('.js-background').css('background-image', 'url("' + bgSrc + '")');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question