С некоторыми быстрыми jQuery
мы можем
fadeIn
.modal
с id
. - нацелить на все модальные дивы, которые этого не имеютid и
fadeOut
с помощью jquery-ui
В чате было установлено, что вы используете $ j, чтобы избежать конфликтов jQuery, в этой версии изменен объект jQuery
$j = jQuery.noConflict();
function showHide(d) {
$j(`#${d}`).fadeIn("fast"); //show the selected one in case its hidden
$j(`div.modal:not([id=${d}])`).fadeOut("slow"); //fadeOut the others.
}
div[id] {
border: 1px solid black;
width: 150px;
height: 150px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<!-- modals -->
<div id="tree-surgery" class="modal">
</div>
<!-- end .modal -->
<div id="site-clearance" class="modal">
</div>
<!-- end .modal -->
<div id="stump-grinding" class="modal">
</div>
<!-- end .modal -->
<div id="hedge-trimming" class="modal">
</div>
<!-- end .modal -->
<div id="conservation" class="modal">
</div>
<!-- end .modal -->
<div id="commercial" class="modal">
</div>
<!-- end .modal -->
<!-- icons -->
<br />
<a href="javascript:showHide('tree-surgery');">
<span>Tree Surgery</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('site-clearance');">
<span>Site Clearance</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('stump-grinding');">
<span>Stump Grinding</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('hedge-trimming');">
<span>Hedge Trimming</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('conservation');">
<span>Conservation</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('commercial');">
<span>Commercial</span></div>
</a>
</div>
Стандартный jQuery
function showHide(d) {
$(`#${d}`).fadeIn("fast"); //show the selected one in case its hidden
$(`div.modal:not([id=${d}])`).fadeOut("slow"); //fadeOut the others.
}
div[id] {
border: 1px solid black;
width: 150px;
height: 150px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<!-- modals -->
<div id="tree-surgery" class="modal">
</div>
<!-- end .modal -->
<div id="site-clearance" class="modal">
</div>
<!-- end .modal -->
<div id="stump-grinding" class="modal">
</div>
<!-- end .modal -->
<div id="hedge-trimming" class="modal">
</div>
<!-- end .modal -->
<div id="conservation" class="modal">
</div>
<!-- end .modal -->
<div id="commercial" class="modal">
</div>
<!-- end .modal -->
<!-- icons -->
<br />
<a href="javascript:showHide('tree-surgery');">
<span>Tree Surgery</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('site-clearance');">
<span>Site Clearance</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('stump-grinding');">
<span>Stump Grinding</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('hedge-trimming');">
<span>Hedge Trimming</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('conservation');">
<span>Conservation</span></div>
</a>
</div>
<div class="cell small-12 medium-6 large-4 block">
<a href="javascript:showHide('commercial');">
<span>Commercial</span></div>
</a>
</div>
Версия 1: интерактивные элементы div
$(function() {
$("div.modal").click(function(e) {
//this is the clicked div
showHide(this.id);
$(this).text(this.id); //make the content of the div it's id for demonstration purposes
});
});
function showHide(d) {
var divs = $(`div.modal:not([id=${d}])`).fadeOut("slow");
//$(`#${d}`).fadeIn("fast"); //incase you want to fade the clicked one back in.
}
div[id] {
border: 1px solid black;
width: 150px;
height: 150px;
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<div id="tree-surgery" class="modal"></div>
<div id="site-clearance" class="modal"></div>
<div id="stump-grinding" class="modal"></div>
<div id="hedge-trimming" class="modal"></div>
<div id="conservation" class="modal"></div>
<div id="commercial" class="modal"></div>