美文网首页我爱编程
jQuery:一些js常用动效

jQuery:一些js常用动效

作者: 流蓝浅 | 来源:发表于2018-03-16 20:18 被阅读0次

目录:
1:全选/反选
2:tab 切换
3:下拉菜单
4:图片轮播
5:吸顶效果
6:回到顶部
7:弹出框

1:全选/反选
如果你有JS性能洁癖的话,显然prop的性能更高,因为attr需要访问DOM属性节点,访问DOM是最耗时的。这种情况适用于多选项全选和反选的情况。

大家都知道有的浏览器只要写disabled,checked就可以了,而有的要写成disabled ="disabled",checked="checked",比如用attr("checked")获取checkbox的checked属性时选中的时候可以取到值,值为"checked"但没选中获取值就是undefined

jq提供新的方法“prop”来获取这些属性,就是来解决这个问题的,以前我们使用attr获取checked属性时返回"checked"和"",现在使用prop方法获取属性则统一返回true和false。

那么,什么时候使用attr,什么时候使用prop??
1.添加属性名称该属性就会生效应该使用prop.
2.是有true,false两个属性使用prop.
3.其他则使用attr

$(function() {
                $("#all").click(function() {
                    if ($("#all").prop("checked")) {
                        $(".list li input").prop("checked",true);
                        $("#content").html("反选")
                    } else {
                        $(".list li input").prop("checked",false);
                        $("#content").html("全选")
                    }
                });
                
//              $("input").each(function(index,item) {
//                  console.info(index)
//                  console.info(item)
//              })

                
            });
            
        </script>
    </head>
    <body>
        <input type="checkbox" name="" id="all" value="" checked="true" /><span id="content">全选</span>
        <ul class="list">
            <li><input type="checkbox"  />读书</li>
            <li><input type="checkbox"  />读书</li>
            <li><input type="checkbox"  />读书</li>
            <li><input type="checkbox"  />读书</li>
        </ul>
    </body>

2:tab 切换
要设置display 另外要找关系,精确的找到要切换的子元素
否则可能会串位

   .main-list-content>ul{
                display: none;
           }
           .main-list-content>ul:nth-child(1){
             display: block;
           } 
                $(".main-list>ul>li").mouseenter(function(){
                        $(this).css("background","#558183").siblings().css("background","#91BEDC")
                        $(this).parent().children().eq(4).css("background","white")
                        $(this).parent().parent().siblings().children().eq($(this).index()).show().siblings().hide();
                        
                })

3:下拉菜单
要用到绝对定位和相对定位
父级元素相对定位,现有元素绝对定位才会脱离DOM层,产生效果

注释掉的那种方法容易产生动画时延,所以用了hover效果

    $(function() {
//          $("#menu_bar>dl.menu").mouseenter(function() {
//              $(this).children("dd").show();
//          });
//          
//          $("#menu_bar>dl.menu").mouseleave(function() {
//              $(this).children("dd").hide();
//          })

//          $("#menu_bar>dl.menu").mouseenter(function() {
//              $(this).children("dd").show();
//          }).mouseleave(function() {
//              $(this).children("dd").hide();
//          })
            
            $("#menu_bar>dl.menu").hover(function() {
                $(this).children("dd").show();
            },function() {
                $(this).children("dd").hide();
            })

            // $("#menu_bar>dl.menu").hover(function() {
            //  $(this).children("dd").stop().slideToggle(200);
            // });
    
            
        });

4:图片轮播

图片.png

写这个的时候,出了一点小问题,就是下拉菜单被图片轮播遮挡,用
z-index来控制绝对定位的优先级。

 //图片轮播
                 var index = 0;
                 var time;
                 //边界控制,显示隐藏
                 function change(){
                    if(index >= $(".top-img>ul>li").length){
                        index = 0;
                    }
                    if(index < 0){
                        index = $(".top-img>ul>li").length-1;
                    }
                    // 进行图片切换
                    $(".top-img>ul>li").eq(index).show().siblings().hide()
                    //对小点进行颜色变换 
                    //方式1
//                  $(".top-img>ol>li").eq(index).css("background","#008B8B").siblings().css("background","white")
                    //方式2
                    $(".top-img>ol>li").eq(index).addClass("active").siblings().removeClass("active")
                 }
                 //切换
                 function make(){
                    time = setInterval(function(){
                        index++
                        change()
                    },2000)
                 }
                 
                 make()
                 //按左右点击时需要先清除掉轮播效果,变换图片,然后重新轮播效果
                 function remake(){
                    clearInterval(time)
                    change()
                    make()
                 }
                 
                 //左右点击切换
                 $(".top-img .left").click(function(){
                    index++;
                    change()
                    remake()
                 })
                 
                  $(".top-img .right").click(function(){
                    index--;
                    change()
                    remake()
                 })
                  
                  //点击小点进行切换
                  $(".top-img ol li").click(function(){
                    index = $(this).index()
                    remake()
                  })
                 
            });
            

5:吸顶效果

$(function() {
                // offset().top获取当前标签距离父级的顶部的距离
                var height = $(".target").offset().top;
                console.info(height)
                $(window).scroll(function() {
                    // 获取滚动条的高度
//                  console.log($(window).scrollTop()
                    if ($(window).scrollTop()>= height) {
                        $(".target").css({
                            "position":"fixed",
                            "top": 0
                        }) 
                    } else {
                        $(".target").css("position","static")   
                    }
                    
                })
            });

7:弹出框

    <script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
        <script>
            $(function() {
                $("#closed").on("click",function() {
                    $("#model").hide();
                    $("#box").hide();
                });
                
                $("#model").click(function() {
                    $("#model").hide();
                    $("#box").hide();
                })
            })
            
            function openWindow() {
                $("#model").show();
                $("#box").show();
            }
            
            
            
        </script>
    </head>
    <body>
        <button onclick="openWindow()">弹出框</button>
        阿士大夫撒旦
        
        
        <div id="box">
            <span id="closed">
                X
            </span>
            这个是一个弹出框
        </div>
        <div id="model">
            
        </div>
        
    </body>

相关文章

  • jQuery:一些js常用动效

    目录:1:全选/反选2:tab 切换3:下拉菜单4:图片轮播5:吸顶效果6:回到顶部7:弹出框 1:全选/反选如果...

  • 高性能js动画库velocity.js

    velocity.js官网 一、为什么是velocity不是jquery 时下,如何快速制作js动效,许多人可能会...

  • js jquery的区别

    1. JS / JQuery介绍 Jquery是JS库,何为JS库,即把常用的js方法进行封装,封装到单独的JS文...

  • jQuery选择器

    jQuery是什么? jQuery描述 jQuery是JS的一个库,封装了我们开发过程中常用的一些功能, 方便我们...

  • jQuery-动效

    jQuery显示与隐藏 show([speed,[easing],[fn]])显示隐藏的匹配元素内部的实现原理是根...

  • lyjutils.js javascript 工具类

    收集整理的常用js,部分方法依赖jquery

  • 2020-04-01

    1.常用的js框架以及分别适用的领域jquery:简化了js的一些操作,并且提供了一些非常好用的APIjquery...

  • jQuery与lodash($|_)

    jQuery jQuery 是一个非常经典的 js 库 $是表达符号 常用 jQuery对象DOM操作 jQuer...

  • jQuery介绍及选择器的使用1

    什么是 jQuery jQuery 是 js 的一个库,封装了我们开发过程中常用的一些功能,方便我们调用,提高开发...

  • Jquery总结

    Jquery能实现的JS都能实现,JS实现的Jquery未必能实现 事件 常用的基本事件 鼠标事件mouseent...

网友评论

    本文标题:jQuery:一些js常用动效

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