jQuery: 动画案例二

动画:鼠标移动到一级菜单上时,打开对应的二级菜单,移出时关闭该二级菜单。

使用的jQuery方法:slideDown(), slideUp(), stop(), find(), mouseenter(), mouseleave()。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动画案例二</title>
    <script src="../jquery.min.js"></script>
    <style type="text/css" rel="stylesheet">
        *{
            margin: 0;
            padding:0;
        }
        div{
            width: 400px;
            height: 50px;
            margin: 0 auto;
            top: 200px;
            
            position: relative;
        }
        ul{
            list-style-type: none;
        }
        ul li{
            float: left;
            width: 100px;
            height:30px;
            line-height: 30px;
            text-align:center;
            color: #fff;
            background: #0000ff;
        }
        .item ul{
            display: none;
        }
        .item ul li{
            border: 1px solid grey;
            background: #000092;
        }
    </style>
</head>
<body>
<div>
    <ul>
    <li class="item">首页
        <ul>
            <li>Home</li>
            <li>Home</li>
            <li>Home</li>
        </ul>
    </li>
    <li class="item">新闻
        <ul>
            <li>News</li>
            <li>News</li>
            <li>News</li>
        </ul>
    </li>
    <li class="item">列表
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
    </li>
    <li class="item">联系
        <ul>
            <li>Tel</li>
            <li>Tel</li>
            <li>Tel</li>
        </ul>
    </li>
</ul>
</div>
<script type="text/javascript">
    $(document).ready(function(){
        $(".item").mouseenter(function(){
            $(this).find("ul").stop(false,true).slideDown(500);
        });
        $(".item").mouseleave(function () {//视频教程中使用的是mouseout(),但当鼠标移入二级
                                           //菜单时会出现bug,使用mouseleave()不会出现bug。
            $(this).find("ul").stop(false,true).slideUp(500);
        });
        /*$(".item").mouseover(function(){
            $(this).find("ul").stop(false,true).slideDown(500);
        }).mouseleave(function () {
            $(this).find("ul").stop(false,true).slideUp(500);
        });*/
    });

</script>
</body>

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是几个基于jQuery实现动画或者网页特效的案例: 1. 图片轮播效果 HTML结构: ```html <div class="slider"> <div class="slider-wrapper"> <img src="image1.jpg" alt="Image 1" /> <img src="image2.jpg" alt="Image 2" /> <img src="image3.jpg" alt="Image 3" /> </div> </div> ``` CSS样式: ```css .slider { position: relative; width: 100%; height: 400px; overflow: hidden; } .slider-wrapper { position: absolute; width: 300%; height: 100%; left: 0; top: 0; } .slider-wrapper img { width: 33.333%; height: 100%; float: left; display: block; } ``` JavaScript代码: ```javascript $(function() { var sliderWrapper = $('.slider-wrapper'); var sliderImageWidth = $('.slider-wrapper img').width(); var sliderImageCount = $('.slider-wrapper img').length; var currentImage = 1; sliderWrapper.css('width', sliderImageCount * sliderImageWidth); function slide() { sliderWrapper.animate({left: -sliderImageWidth * currentImage}, function() { currentImage++; if(currentImage === sliderImageCount) { currentImage = 1; sliderWrapper.css('left', 0); } }); } setInterval(slide, 3000); }); ``` 2. 折叠面板效果 HTML结构: ```html <div class="accordion"> <div class="accordion-section"> <h3 class="accordion-header">Section 1</h3> <div class="accordion-content"> <p>Content for section 1</p> </div> </div> <div class="accordion-section"> <h3 class="accordion-header">Section 2</h3> <div class="accordion-content"> <p>Content for section 2</p> </div> </div> <div class="accordion-section"> <h3 class="accordion-header">Section 3</h3> <div class="accordion-content"> <p>Content for section 3</p> </div> </div> </div> ``` CSS样式: ```css .accordion-section { border: 1px solid #ccc; margin-bottom: 10px; } .accordion-header { padding: 10px; margin: 0; background-color: #f0f0f0; cursor: pointer; } .accordion-content { display: none; padding: 10px; } ``` JavaScript代码: ```javascript $(function() { var accordionHeaders = $('.accordion-header'); accordionHeaders.click(function() { var accordionContent = $(this).next('.accordion-content'); if(accordionContent.is(':visible')) { accordionContent.slideUp(); } else { accordionContent.slideDown(); } }); }); ``` 3. 滚动监听效果 HTML结构: ```html <nav class="menu"> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> </ul> </nav> <section id="section1"> <h2>Section 1</h2> <p>Content for section 1</p> </section> <section id="section2"> <h2>Section 2</h2> <p>Content for section 2</p> </section> <section id="section3"> <h2>Section 3</h2> <p>Content for section 3</p> </section> ``` CSS样式: ```css .menu { position: fixed; top: 0; left: 0; } .menu ul { list-style: none; margin: 0; padding: 0; } .menu li { display: inline-block; margin-right: 10px; } section { height: 500px; margin-top: 60px; border: 1px solid #ccc; padding: 20px; } ``` JavaScript代码: ```javascript $(function() { var menu = $('.menu'); var menuLinks = $('.menu a'); var sections = $('section'); $(window).scroll(function() { var currentScroll = $(this).scrollTop(); sections.each(function() { var sectionTop = $(this).offset().top - 100; var sectionBottom = sectionTop + $(this).height(); if(currentScroll >= sectionTop && currentScroll <= sectionBottom) { var sectionId = $(this).attr('id'); var menuLink = menu.find('a[href="#' + sectionId + '"]'); menuLinks.removeClass('active'); menuLink.addClass('active'); } }); }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值