Jquery .Animate 宽度问题
预先感谢您对此提供的任何帮助,我会尽力解释清楚。
我有一个宽度为 1000 像素、高度为 220 像素的容器,其中我将拥有三列,高度为 220 像素,但宽度不同(77 像素、200 像素和 300 像素)。当您单击一个 div 时,它将打开为固定大小(每个 div 都相同,例如 400px),而其他未单击的 div 将缩小回原始大小(77px、200px 和 300px)。就像不同宽度的手风琴一样..
我的jquery已经到达那里,但还没有完全完成,我知道如何在点击时创建一个事件,我知道从那里我需要将所有东西缩小,除了我点击的那一个回到原来的大小。我完成了,但使单击的部分扩展至所需的大小。
$(document).ready(function(){
// Your code here
$('.section').click(function() {
$('.section').not(this).animate({width:"77px"},400);
//alert('clicked!');
$(this).animate({width:"400px"},400);
});
});
<div id="pictureNavContainer">
<div class="section pictureSectionOne" id="1"></div>
<div class="section pictureSectionTwo" id="2"></div>
<div class="section pictureSectionThree" id="3"></div>
</div>
<style>
#pictureNavContainer{background-color:#262626; width:1000px; height:220px; overflow:hidden;}
.pictureSectionOne{float:left; background:yellow; height:220px; width:77px;}
.pictureSectionTwo{float:left; background:red; height:220px; width:177px;}
.pictureSectionThree{float:left; background:blue; height:220px; width:400px;}
</style>
我想出了一种解决方案:
<script>
$(document).ready(function(){
// Your code here
$('.section').click(function() {
$('#1').animate({width:"50px"},400);
$('#2').animate({width:"100px"},400);
$('#3').animate({width:"200px"},400);
//alert('clicked!');
$(this).animate({width:"400px"},400);
});
});
</script>
但是代码不是很好..但它
有效:
$(function(){
var $sections = $('.section');
var orgWidth = [];
var animate = function() {
var $this = $(this);
$sections.not($this).each(function(){
var $section = $(this),
org = orgWidth[$section.index()];
$section.animate({
width: org
}, 400);
});
$this.animate({
width: 400
}, 400);
};
$sections.each(function(){
var $this = $(this);
orgWidth.push($this.width());
$this.click(animate);
});
});
Thanks in advance for any-help with this, I'll try and explain it well.
I have a container of 1000px width and 220px height, in this I will have three columns 220px in height but with different widths (77px, 200px and 300px). When you click one div it will open to a fixed size (the same for each, say 400px) and the others which are not clicked will shrink back to their original sizes (77px, 200px and 300px). Like an accordian with different widths..
My jquery is getting there but not quite, I know how to create an Event on click, I know from there I need to shrink everything but the one I clicked back to their orignal size. I finish but making the one clicked expand to the size it needs to be.
$(document).ready(function(){
// Your code here
$('.section').click(function() {
$('.section').not(this).animate({width:"77px"},400);
//alert('clicked!');
$(this).animate({width:"400px"},400);
});
});
<div id="pictureNavContainer">
<div class="section pictureSectionOne" id="1"></div>
<div class="section pictureSectionTwo" id="2"></div>
<div class="section pictureSectionThree" id="3"></div>
</div>
<style>
#pictureNavContainer{background-color:#262626; width:1000px; height:220px; overflow:hidden;}
.pictureSectionOne{float:left; background:yellow; height:220px; width:77px;}
.pictureSectionTwo{float:left; background:red; height:220px; width:177px;}
.pictureSectionThree{float:left; background:blue; height:220px; width:400px;}
</style>
I figured a kind of solution:
<script>
$(document).ready(function(){
// Your code here
$('.section').click(function() {
$('#1').animate({width:"50px"},400);
$('#2').animate({width:"100px"},400);
$('#3').animate({width:"200px"},400);
//alert('clicked!');
$(this).animate({width:"400px"},400);
});
});
</script>
But the code isnt very good.. but it works
This:
$(function(){
var $sections = $('.section');
var orgWidth = [];
var animate = function() {
var $this = $(this);
$sections.not($this).each(function(){
var $section = $(this),
org = orgWidth[$section.index()];
$section.animate({
width: org
}, 400);
});
$this.animate({
width: 400
}, 400);
};
$sections.each(function(){
var $this = $(this);
orgWidth.push($this.width());
$this.click(animate);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我已经开始工作了。喜欢这个吗?
但是,这不会将单击的元素设置为固定大小,而是设置为剩余的大小。如果您将单击的元素调整为固定大小,则三列将变得更宽或更薄,然后 1000px,这真的是您想要的吗?
这里是一个更新的示例。这正是您所要求的,但我不确定这是否是您想要的。
带有内嵌注释的代码:
Ok, I think I got it to work. Like this?
However, this doesn't set the clicked element to a fixed size but to the size that is left. If you resize the clicked element to a fixed size the three columns will either become wider or thinner then 1000px, is that really what you want?
Here is an updated example. This does exactly what you asked for, but I'm not sure that this is what you want.
Code with inline comments: