美文网首页
jQuery属性动画循环

jQuery属性动画循环

作者: jackmanzhang | 来源:发表于2018-09-19 23:13 被阅读0次

    事件委托

    事件委托:方法delegate,只绑定一次事件,冒泡触发
    参数:
    selector选择器:写入ul下面的所有要发生事件的元素,多个元素用空格隔开,例如‘li a span’
    eventType事件
    function要执行的操作

    jQuery特殊效果

    fadeOut() 淡出
    fadeToggle() 切换淡入淡出
    hide() 隐藏元素
    show() 显示元素
    toggle() 依次展示或隐藏某个元素
    slideDown() 向下展开
    slideUp() 向上卷起
    slideToggle() 依次展开或卷起某个元素

    jQuery链式调用

    $('#div1') // id为div1的元素
    .children('ul') //该元素下面的ul子元素
    .slideDown('fast') //高度从零变到实际高度来显示ul元素
    .parent() //跳到ul的父元素,也就是id为div1的元素
    .siblings() //跳到div1元素平级的所有兄弟元素
    .children('ul') //这些兄弟元素中的ul子元素
    .slideUp('fast'); //高度实际高度变换到零来隐藏ul元素

    jQuery动画

    $(".box").animate({
        width:'500px',
        height:'500px'
    },1000,'swing')//第二个参数是运行时间,第三个是样式,默认是swing,两边慢,linear(匀速)
    

    滚动事件

    $(window).scroll(function(){  
    $(document).scrollTop();  
    $(document).scrollLeft();
    })
    

    jQuery事件

    blur() 元素失去焦点
    focus() 元素获得焦点
    change() 表单元素的值发生变化
    click() 鼠标单击
    dblclick() 鼠标双击
    mouseover() 鼠标进入(进入子元素也触发)
    mouseout() 鼠标离开(离开子元素也触发)
    mouseenter() 鼠标进入(进入子元素不触发)
    mouseleave() 鼠标离开(离开子元素不触发)
    hover() 同时为mouseenter和mouseleave事件指定处理函数
    mouseup() 松开鼠标
    mousedown() 按下鼠标
    mousemove() 鼠标在元素内部移动
    keydown() 按下键盘
    keypress() 按下键盘
    keyup() 松开键盘
    load() 元素加载完毕
    ready() DOM加载完成
    resize() 浏览器窗口的大小发生改变
    scroll() 滚动条的位置发生变化
    select() 用户选中文本框中的内容
    submit() 用户递交表单
    toggle() 根据鼠标点击的次数,依次运行多个函数
    unload() 用户离开页面

    节点操作

    创建节点

    var div =('<div>');
    var div2 =('<div>这是一个div元素</div>');

    插入节点
    1. append()和appendTo():在现存元素的内部,从后面插入元素
    2. prepend()和prependTo():在现存元素的内部,从前面插入元素
    3. after()和insertAfter():在现存元素的外部,从后面插入元素
    4. before()和insertBefore():在现存元素的外部,从前面插入元素
    删除节点

    $('#div1').remove();

    计划列表

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="js/jquery-1.11.0.js"></script>
        <style type="text/css">
            .list_con {
                width: 400px;
                margin: 50px auto 0;
            }
    
            .inputtxt {
                width: 350px;
                height: 30px;
                border: 1px solid #ccc;
                padding: 0px;
                text-indent: 10px;
    
            }
    
            .inputbtn {
                width: 40px;
                height: 32px;
                padding: 0px;
                border: 1px solid #ccc;
            }
    
            .list {
                margin: 0;
                padding: 0;
                list-style: none;
                margin-top: 20px;
            }
    
            .list li {
                height: 30px;
                line-height: 30px;
                border-bottom: 1px solid #ccc;
            }
    
            .list li span {
                float: left;
            }
    
            .list li a {
                float: right;
                text-decoration: none;
                margin: 0 10px;
            }
        </style>
    </head>
    <body>
    <div class="list_con">
        <h2>To do list</h2>
        <input type="text" name="" id="txt1" class="inputtxt">
        <input type="button" name="" value="增加" id="btn1" class="inputbtn">
    
        <ul id="list" class="list">
    
            <li><span>学习html</span>
                <a href="javascript:;" class="up"> ↑ </a>
                <a href="javascript:;" class="down"> ↓ </a>
                <a href="javascript:;" class="del">删除</a></li>
            <li><span>学习css</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down"> ↓ </a><a
                    href="javascript:;" class="del">删除</a></li>
            <li><span>学习javascript</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down">
                ↓ </a><a href="javascript:;" class="del">删除</a></li>
    
        </ul>
    
    </div>
    <script>
        $("#btn1").click(function () {
            var value = $("#txt1").val();
            if (value == "") {
                alert("不能为空")
                return
            }
            var li = $('<li><span>' + value + '</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down"> ↓ </a><a href="javascript:;" class="del">删除</a></li>')
            li.appendTo("#list")
            $("#txt1").val("");
        })
    
    
        $('#list').delegate("a", "click", function () {
            var c=$(this).attr('class')
            if(c=="del"){
                $(this).parent().remove()
            }else if(c=="up"){
                if($(this).parent().prev().length==0){
                    alert('到顶')
                    return
                }
                $(this).parent().insertBefore($(this).parent().prev())
            }else{
                if($(this).parent().next().length==0){
                    alert('到底部了')
                    return
                }
                 $(this).parent().insertAfter($(this).parent().next())
            }
    
    
                })
    
    
    
    </script>
    
    </body>
    </html>
    

    层级菜单

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="js/jquery-1.11.0.js"></script>
    
        <style type="text/css">
            body{
                font-family:'Microsoft Yahei';
            }
            body,ul{
                margin:0px;
                padding:0px;
            }
            ul{list-style:none;}
            .menu{
                width:200px;
                margin:20px auto 0;
            }
            .menu .level1,.menu li ul a{
                display:block;
                width:200px;
                height:30px;
                line-height:30px;
                text-decoration:none;
                background-color:#3366cc;
                color:#fff;
                font-size:16px;
                text-indent:10px;
            }
            .menu .level1{
                border-bottom:1px solid #afc6f6;
    
            }
            .menu li ul a{
                font-size:14px;
                text-indent:20px;
                background-color:#7aa1ef;
    
            }
            .menu li ul li{
                border-bottom:1px solid #afc6f6;
            }
            .menu li ul{
                display:none;
            }
            .menu li ul.current{
                display:block;
            }
            .menu li ul li a:hover{
                background-color:#f6b544;
            }
        </style>
    
    
        <script>
            $(function () {
                // $(".level1").click(function () {
                //     $(this).next().addClass('current')//添加样式
                // })
    
                // $(".level1").click(function () {
                //     $(this).next().toggleClass('current')//切换样式
                // })
    
                //  $(".level1").click(function () {
                //     $(this).next().slideDown('current')//慢慢展开
                // })
                //  $(".level1").click(function () {
                //     $(this).next().slideUp('current')//慢慢收起
                // })
                //   $(".level1").click(function () {
                //     $(this).next().slideToggle('current')//慢慢切换
                // })
    
                  $(".level1").click(function () {
                    $(this).next().slideDown('current').parent().siblings().children('ul').slideUp('current')
                })
    
            })
    
        </script>
    
    </head>
    <body>
    <ul class="menu">
            <li>
                <a href="#" class="level1">水果</a>
                <ul class="current">
                    <li><a href="#">苹果</a></li>
                    <li><a href="#">梨子</a></li>
                    <li><a href="#">葡萄</a></li>
                    <li><a href="#">火龙果</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="level1">海鲜</a>
                <ul>
                    <li><a href="#">蛏子</a></li>
                    <li><a href="#">扇贝</a></li>
                    <li><a href="#">龙虾</a></li>
                    <li><a href="#">象拔蚌</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="level1">肉类</a>
                <ul>
                    <li><a href="#">内蒙古羊肉</a></li>
                    <li><a href="#">进口牛肉</a></li>
                    <li><a href="#">野猪肉</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="level1">蔬菜</a>
                <ul>
                    <li><a href="#">娃娃菜</a></li>
                    <li><a href="#">西红柿</a></li>
                    <li><a href="#">西芹</a></li>
                    <li><a href="#">胡萝卜</a></li>
                </ul>
            </li>
            <li>
                <a href="#" class="level1">速冻</a>
                <ul>
                    <li><a href="#">冰淇淋</a></li>
                    <li><a href="#">湾仔码头</a></li>
                    <li><a href="#">海参</a></li>
                    <li><a href="#">牛肉丸</a></li>
                </ul>
            </li>
        </ul>
    </body>
    </html>
    

    手风琴

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    
            body {
                font-size: 12px;
            }
    
            #accordion {
                width: 727px;
                height: 350px;
                margin: 100px auto 0 auto;
                position: relative;
                overflow: hidden;
                border: 1px solid #CCC;
            }
    
            #accordion ul {
                list-style: none;
            }
    
            #accordion ul li {
                width: 643px;
                height: 350px;
                position: absolute;
                background: #FFF;
            }
    
            #accordion ul li span {
                display: block;
                width: 20px;
                height: 350px;
                float: left;
                text-align: center;
                color: #FFF;
                padding-top: 5px;
                cursor: pointer;
            }
    
            #accordion ul li img {
                display: block;
                float: right;
            }
    
            .bar01 {
                left: 0px;
            }
    
            .bar02 {
                left: 643px;
            }
    
            .bar03 {
                left: 664px;
            }
    
            .bar04 {
                left: 685px;
            }
    
            .bar05 {
                left: 706px;
            }
    
            .bar01 span {
                background: #09E0B5;
            }
    
            .bar02 span {
                background: #3D7FBB;
            }
    
            .bar03 span {
                background: #5CA716;
            }
    
            .bar04 span {
                background: #F28B24;
            }
    
            .bar05 span {
                background: #7C0070;
            }
        </style>
        <script src="js/jquery-1.11.0.js"></script>
    </head>
    <body>
    <div id="accordion">
        <ul>
            <li class="bar01"><span>非洲景色01</span><img src="image/001.jpg"/></li>
            <li class="bar02"><span>非洲景色02</span><img src="image/002.jpg"/></li>
            <li class="bar03"><span>非洲景色03</span><img src="image/003.jpg"/></li>
            <li class="bar04"><span>非洲景色04</span><img src="image/004.jpg"/></li>
            <li class="bar05"><span>非洲景色05</span><img src="image/005.jpg"/></li>
        </ul>
    </div>
    <script>
        $("#accordion li").click(function () {
            $(this).animate({
                left: $(this).index() * 21
            })
    
            $(this).prevAll().each(function () {
                $(this).animate({
                    left: $(this).index() * 21
                })
            })
    
            $(this).nextAll().each(function () {
                $(this).animate({
                    left: 706-(4-$(this).index())*21
                })
            })
    
    
        })
    
    </script>
    </body>
    </html>
    

    无缝滚动

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="js/jquery-1.11.0.js"></script>
        <style type="text/css">
            body,ul,li{margin:0;padding:0}
            ul{list-style:none;}
            .slide{
                width:500px;
                height:100px;
                border:1px solid #ddd;
                margin:20px auto 0;
                position:relative;
                overflow:hidden;
            }
            .slide ul{
                position:absolute;/*相对于slide进行绝对定位*/
                width:1000px;/*比slide宽度大一倍,做这种连续滚动效果的时候,要在后面把内容复制一份*/
                height:100px;
                left:0;/*可以改变该值让其动起来*/
                top:0;
            }
            .slide ul li{
                width:90px;
                height:90px;
                margin:5px;
                background-color:#ccc;
                line-height:90px;
                text-align: center;
                font-size:30px;
                float:left;
            }
            .btns{
                width:500px;
                height:50px;
                margin:10px auto 0;
            }
        </style>
    </head>
    <body>
    <div class="btns">
            <input type="button" name="" value="向左" id="btn1">
            <input type="button" name="" value="向右" id="btn2">
        </div>
        <div class="slide" id="slide">
            <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
            </ul>
        </div>
    <script>
    
            var $ul=$("#slide ul")
            // alert($ul.length)
            // $("#slide ul").html($("#slide ul").html()+$("#slide ul").html())
            $ul.html($ul.html() + $ul.html());
            left=0;
            count=2
            var time=setInterval(function () {
                if(left<-500){
                    left=0
                }
                if(left>0){
                    left=-500
                }
                left-=count;
                $ul.css({
                    left:left
                })
            },30)
        $('#btn1').click(function () {
            count=2
        })
    
         $('#btn2').click(function () {
            count=-2
        })
        $("#slide").mouseover(function () {
            clearInterval(time)
        })
        $("#slide").mouseleave(function () {
            time=setInterval(function () {
                if(left<-500){
                    left=0
                }
                if(left>0){
                    left=-500
                }
                left-=count;
                $ul.css({
                    left:left
                })
            },30)
        })
    
        
    </script>
    </body>
    </html>
    

    置顶菜单

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style type="text/css">
            body{margin:0px;}
            .logo_bar{
                width:960px;
                height:200px;
                background-color:#f0f0f0;
                margin:0 auto;
            }
            .menu,.menu_pos{
                width:960px;
                height:50px;
                margin:0 auto;
                background-color:gold;
                text-align:center;
                line-height:50px;
            }
            .menu_pos{
                display:none;
            }
            .down_con{
                width:960px;
                height:1800px;
                margin:0 auto;
            }
    
            .down_con p{
                margin-top:100px;
                text-align:center;
            }
            .totop{
                width:50px;
                height:50px;
                background:url(image/up.png) center center no-repeat #000;
                border-radius:50%;
                position:fixed;
                right:50px;
                bottom:50px;
                display: none;
            }
        </style>
        <script src="js/jquery-1.11.0.js"></script>
    </head>
    <body>
    <div class="logo_bar">顶部logo</div>
        <div class="menu">置顶菜单</div>
        <!-- 占位层,如果菜单显示,它就隐藏,菜单浮动,它就显示,顶替菜单的位置,避免页面跳动 -->
        <div class="menu_pos"></div>
        <div class="down_con">
            <p style="color: red">网站主内容</p>
            <p>网站主内容</p>
            <p>网站主内容</p>
            <p>网站主内容</p>
            <p>网站主内容</p>
        </div>
        <!-- 点击超链接时,不想跳转页面的href写法 -->
        <a href="javascript:;" class="totop"></a>
    </body>
    <script>
        $(window).scroll(function() {
            var nowTop = $(document).scrollTop();
            // console.log(nowTop)
            if(nowTop>200){
                $(".menu").css({
                    position:'fixed',
                    top:0,
                    left:'50%',
                    marginLeft:-480
                })
                 $(".menu_pos").show()
            }else{
               $(".menu").css({
                    position:'static',
                   marginLeft:'auto'
                })
                $(".menu_pos").css({
                     display:'none'
                 })
            }
            if(nowTop>400){
                // $(".totop").fadeToggle()
                 $(".totop").fadeIn()
                // $(".totop").show()
            }else{
                  $(".totop").fadeOut()
            }
    
    
    
    
        })
        $('.totop').click(function() {
                    $('html,body').animate({scrollTop: 0});
                });
    </script>
    </html>
    

    相关文章

      网友评论

          本文标题:jQuery属性动画循环

          本文链接:https://www.haomeiwen.com/subject/uxiwnftx.html