上下文:我試圖在鼠標(biāo)經(jīng)過時替換一個圖像,在鼠標(biāo)離開時返回原始圖像。當(dāng)我使用圖像src的原始鏈接時,它是有效的。但我的問題是,我將有多個部分。我知道我可以通過在每個原始src上制作一個腳本并在那里輸入圖像鏈接來使它工作,但是有沒有一種方法可以使它返回到原始img src而不添加img的硬鏈接呢?
<div class="col-12 text-center">
<h3>TITLE</h3>
<div class="name-wrapper">
<span id="name1" class="some-name opacity-0">Name 1</span>
<span id="name2" class="some-name opacity-0">Name 2</span>
</div>
</div>
<div class="col-12">
<div class="img-set">
<img src="images/2023/some-img.jpg" alt="Some Image" />
</div>
<div class="hotspots">
<a href="#" data-src="images/2023/some-img2.jpg" data-target="name1">test1</a>
<a href="#" data-src="images/2023/some-img3.jpg" data-target="name2">test2</a>
</div>
</div>
$(".hotspots a").mouseover( function() {
var value = $(this).attr('data-src');
$(".img-set img").attr("src", value);
var target = $(this).data("target");
var targetName = document.getElementById(target);
if($(targetName).attr('id') == target) {
$("#" + target).removeClass("opacity-0");
}
})
$(".hotspots a").mouseout( function() {
$( '.img-set img' ).attr("src","images/2023/some-img.jpg");
var target = $(this).data("target");
var targetName = document.getElementById(target);
if($(targetName).attr('id') == target) {
$("#" + target).addClass("opacity-0");
}
});
我怎樣才能使它不是放入實際的src,而是原始的src呢?
所以我試著用這個,但這一切都不起作用了
$(document).ready(function() {
var original = document.getElementsByClassName(".img-set img").getAttribute("src");
$(".hotspots a").mouseover( function() {
var value = $(this).attr('data-src');
$(".img-set img").attr("src", value);
var target = $(this).data("target");
var targetName = document.getElementById(target);
if($(targetName).attr('id') == target) {
$("#" + target).removeClass("opacity-0");
}
})
$(".hotspots a").mouseout( function() {
$( '.img-set img' ).attr("src", original);
var target = $(this).data("target");
var targetName = document.getElementById(target);
if($(targetName).attr('id') == target) {
$("#" + target).addClass("opacity-0");
}
});
})
問題是你打了電話
document.getElementsByClassName(".img-set img")
這與您實際需要的元素不匹配。(您得到空值)
請改用
document.querySelector(".img-set img")
或者
$(".img-set img")
有點偏左,但是...有一種方法只使用CSS:has()偽類就可以做到這一點。將兩幅圖像放入你的標(biāo)記中,然后在:懸停選擇器上切換顯示模式。
唯一的警告是,目前firefox或一些舊瀏覽器不支持這一功能。
這里有一個凱文·鮑威爾關(guān)于:has()的很好的視頻,我希望它能解釋一些事情。
下面標(biāo)記的代碼:
#image2 {
display:none;
}
/*on the body element, if the a tags in the div with class hotspot is hovered then on the element with ID image2 set display to intial */
body:has(.hotspots a:hover) #image2 {
display:initial;
}
/*similarly for the other one, display none */
body:has(.hotspots a:hover) #image1 {
display:none;
}
<div class="col-12 text-center">
<h3>TITLE</h3>
<div class="name-wrapper">
<span id="name1" class="some-name opacity-0">Name 1</span>
<span id="name2" class="some-name opacity-0">Name 2</span>
</div>
</div>
<div class="col-12">
<div class="img-set">
<!-- added both images but we hide the 2nd one intially -->
<img id='image1' src="https://picsum.photos/id/237/200/200">
<img id='image2' src="https://picsum.photos/id/238/200/200">
</div>
<div class="hotspots">
<a href="#" id='hover1'>test1</a>
<a href="#" id='hover2'>test2</a>
</div>
</div>
只需在第一次懸停時保存原始src,并將其設(shè)置為事件mouseleave:
$(document).on('mouseenter', '.hotspots a', function() {
const t = $(this);
const img = t.closest('.hotspots-wrapper').find('.img-set img');
if ( !img.data('src') ) {
img.data( 'src', img.attr('src') );
}
img.attr( 'src', t.data('src') );
})
$(document).on('mouseleave', '.hotspots a', function() {
const img = $(this).closest('.hotspots-wrapper').find('.img-set img');
img.attr( 'src', img.data('src') );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hotspots-wrapper">
<div class="img-set">
<img src="https://picsum.photos/id/230/200/300" alt="Some Image">
</div>
<div class="hotspots">
<a href="#" data-src="https://picsum.photos/id/231/200/300">test1</a>
<a href="#" data-src="https://picsum.photos/id/232/200/300">test2</a>
</div>
</div>
你可以只使用jQuery而不使用javascript來選擇原始圖像src。
$(document).ready(function() {
var original = $(".img-set img").attr("src");
$(".hotspots a").mouseover( function() {
var value = $(this).attr('data-src');
$(".img-set img").attr("src", value);
var target = $(this).data("target");
var targetName = document.getElementById(target);
if($(targetName).attr('id') == target) {
$("#" + target).removeClass("opacity-0");
}
})
$(".hotspots a").mouseout( function() {
$( '.img-set img' ).attr("src", original);
var target = $(this).data("target");
var targetName = document.getElementById(target);
if($(targetName).attr('id') == target) {
$("#" + target).addClass("opacity-0");
}
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-12 text-center">
<h3>TITLE</h3>
<div class="name-wrapper">
<span id="name1" class="some-name opacity-0">Name 1</span>
<span id="name2" class="some-name opacity-0">Name 2</span>
</div>
</div>
<div class="col-12">
<div class="img-set">
<img src="https://picsum.photos/id/230/200/300" alt="Some Image" />
</div>
<div class="hotspots">
<a href="#" data-src="https://picsum.photos/id/231/200/300" data-target="name1">test1</a>
<a href="#" data-src="https://picsum.photos/id/232/200/300" data-target="name2">test2</a>
</div>
</div>