gpt4 book ai didi

javascript - 点击滑动改变div

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:30 25 4
gpt4 key购买 nike

我有这个代码:

<a onclick="show('div1')">Show Div 1</a>
<a onclick="show('div2')">Show Div 2</a>`

<div id="div1">This is Div 1</div>
<div id="div2" style="display: none;">This is hidden Div 2</div>

<script>
var currentDiv = document.getElementById("div1");
function show(divID) {
var div = document.getElementById(divID);
currentDiv.style.display = "none";
div.style.display = "block";
currentDiv = div;
}
</script>

我想知道我是否可以在此脚本中添加过渡,以便一个 div 淡出/滑出而另一个 div 淡出/滑入。提前致谢。

最佳答案

试试这个

jQuery(function($) {

$('a.panel').click(function() {
var $target = $($(this).attr('href')),
$other = $target.siblings('.active');

if (!$target.hasClass('active')) {
$other.each(function(index, self) {
var $this = $(this);
$this.removeClass('active').animate({
left: $this.width()
}, 500);
});

$target.addClass('active').show().css({
left: -($target.width())
}).animate({
left: 0
}, 500);
}
});

});
#left, #right {
position: relative;
float: left;
margin: 0 5px 0 0;
border: 1px solid black;
width: 200px;
height: 300px;
overflow: hidden;
}

div.panel {
position: absolute;
height: 100%;
width: 100%;
display: none;
}
<div id="left">
<a href="#target1" class="panel">Target 1</a><br/>
<a href="#target2" class="panel">Target 2</a><br/>
<a href="#target3" class="panel">Target 3</a><br/>
</div>

<div id="right">
<div class="panel" id="target1" style="background:green">Target 1</div>
<div class="panel" id="target2" style="background:red">Target 2</div>
<div class="panel" id="target3" style="background:yellow">Target 3</div>
</div>

演示 Here

希望这对您有所帮助。

关于javascript - 点击滑动改变div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29205390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com