根據用戶點擊的位置,我需要隱藏和顯示一些圖像。 .img-active
類表示默認顯示的圖像,.img-inactive
類表示圖像隱藏。當你點擊錨時,這些圖像必須交換(顯示一個並隱藏另一個)。 顯然,如果你點擊另一個錨點,你最後點擊的那個錨點必須重新設置爲默認狀態。單擊一組錨點元素時切換圖像
但我現在有一個問題,該功能只適用於每個錨點的第一次點擊。當第二次嘗試時,你會看到它被打破。
在視頻中,您會看到每次點擊第二次後每個圓都有邊框,我這樣做只是爲了區分if
條件。
這是你可以看到在視頻的HTML:
<a class="fig-8" data-tab="tab1">
<img src="path/researchicon-x.png" alt="" class="pulse img-active">
<img src="path/selected-x.png" alt="" class="img-inactive">
</a>
<a class="fig-8" data-tab="tab2">
<img src="path/WideRange.png" alt="" class="pulse img-active">
<img src="path/selected-x.png" alt="" class="img-inactive">
</a>
<a class="fig-8" data-tab="tab3">
<img src="path/SSI_toolbox.png" alt="" class="pulse img-active">
<img src="path/selected-x.png" alt="" class="img-inactive">
</a>
<a class="fig-8" data-tab="tab4">
<img src="path/PricingIcon.png" alt="" class="pulse img-active">
<img src="path/selected-x.png" alt="" class="img-inactive">
</a>
這裏是JS功能:
var iconTabs = function() {
$('.tabs1 a').on('click', function() {
var $tabContainer = $(this).parents('.tab-container');
var tabId = $(this).data('tab');
$tabContainer.find('.icons-tabs a').addClass('inactive');
$(this).removeClass('inactive');
// that functionality begins here!!!
$('.tabs1 a').not(this).find('.img-inactive').hide();
$('.tabs1 a').not(this).find('.img-active').show();
var active;
if ($(this).hasClass('selected-shown')) {
active = '.img-inactive';
$(this).find('.img-active').css('border', '5px solid green');
} else {
active = '.img-active';
$(this).find('.img-active').show();
$(this).find('.img-active').css('border', '4px solid red');
}
var show;
if ($(this).hasClass('selected-shown')) {
show = '.img-active';
$(this).find(show).css('border', '3px solid blue');
} else {
show = '.img-inactive';
$(this).removeClass('selected-shown');
$(this).find('.img-active').css('border', '6px solid orange');
}
$(this).addClass('selected-shown').find(show).show();
$(this).find(active).hide();
});
};
這.selected-shown
只是一個標誌,我添加到父元素來檢查我點擊過的錨點。
有什麼建議嗎?
你是神。謝謝! – TheUnnamed