怎么在JQuery中使用animate实现一个动画?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
滑动选项卡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
.btns input{
width: 100px;
height: 40px;
background-color: grey;
border: 0;
}
.btns .current{
background-color: gold;
}
.cons .slides div{
width: 500px;
height: 300px;
background-color: gold;
/*display: none;!*整体都不显示了*!*/
text-align: center;
line-height: 300px;
font-size: 30px;
float: left;/*把三个div由隐藏改为浮动*/
}
.cons{
width: 500px;
height: 300px;
overflow: hidden; /*超过cons的slide隐藏*/
position: relative;/*相对于slide绝对定位*/
}
.slides{
width: 1500px;
height: 300px; /*把slide加长*/
position: absolute;/*相对于cons相对定位*/
}
.cons .active{
display: block;
}
</style>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var $btn=$('.btns input');
var $slides=$('.cons .slides');
// alert($btn.length);
// alert($div.length);
$btn.click(function () {
// 我点击哪一个按钮,$(this)就指的是谁,而this
//指的是原生的,$(this)指的是JQuery的
// $(this).siblings().removeClass('current');
// $(this).addClass('current');//可以用链式调用
$(this).addClass('current').siblings().removeClass('current');
// var num=$(this).index();
// $div.eq($(this).index()).addClass('active').sibling().removeClass('active');
$slides.animate({left:(-500*$(this).index())});
})
})
</script>
</head>
<body>
<div class="btns">
<input type="button" name="" value="01" class="current">
<input type="button" name="" value="02">
<input type="button" name="" value="03">
</div>
<div class="cons">
<div class="slides">
<div >选项卡1的内容</div>
<div>选项卡2的内容</div>
<div>选项卡3的内容</div>
</div>
</div>
</body>
</html>
将slides下面的div由隐藏改为浮动,将cons设置成绝对定位,将slides改为相对定位。超过cons的slides隐藏。
点击事件发生之后,相对定位改变。
animate动画
$div=$('#div1');
$div.animate({
width:300,
height:300
},1000,'swing',function () {
alert('done');
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$box=$('.box');
$box.animate({
width:300,
height:300
},1000,'swing',function () {
alert('done');
})
})
</script>
<style type="text/css">
.box{
width: 100px;
height: 100px;
background-color: gold;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
关于怎么在JQuery中使用animate实现一个动画问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。