JQuery基础

  jQuery介绍

是一个轻量级的js框架/库,其宗旨是write less do more。

  jQuery对象

js的对象叫做dom对象

使用jQuery框架产生的对象是jQuery对象,是对dom对象的包装。jQuery下的对象方法和dom对象方法不能混用,只能各用个的。

约定:如果获取的是 jQuery 对象, 那么要在变量前面加上$

//基本语法
$(selector).action()      //即选择器选择生成对象,对象调用方法

jQuery中文手册:http://jquery.cuishifeng.cn/

对象转换

var $variable = jQuery 对象
var variable = DOM 对象

$variable[0]    jquery对象按索引取出来的是dom对象,如:     
  $("#msg").html()
  $("#msg")[0].innerHTML

  查找操作对象

 选择器

 查找要操作的标签,生成jquery对象

 基本选择器
$("*")
$("#id")
$(".class")
$("element")
$(".class,p,div")
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<p id="p1">PPP</p>
<div class="c1">DIV</div>
<div class="c1">DIV</div>
<div class="c1">DIV</div>
<span class="d1">SPAN</span>
<span class="c2">SPAN</span>
<a href="">AAAA</a>

<script src="jquery-3.2.1.js"></script> //导入jquery文件
<script>
    //    基本选择器
    $("#p1").css("color","red") //按照id选择
    $(".d1").css({"color":"green","fontSize":"50px"}) //按照class选择
    $("div").css({"color":"yellow","fontSize":"35px"}) //按照标签名div选择
    //$("*").css({"color":"blue","fontSize":"30px"}) //body下的所有标签
    $(".c2,a").css({"color":"gray","fontSize":"30px"}) //多个条件用空格隔开


</script>
</body>
</html>

基本选择器示例
基本选择器示例

注意,获取的jquery对象是一个集合,jquery对象在做方法调用进行属性操作的时候,会把集合里的所有元素都循环一次执行,想想js代码的循环,尼玛...

 层级选择器
$(".outer div")   //后代
$(".outer>div")   //子代
$(".outer+div")   //毗邻
$(".outer~div")   //普通兄弟

同css组合选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<p>p6</p>
<div class="outer">
    <div class="inner">
        <p>p3</p>
        <div>DIV</div>
    </div>
    <p>p2</p>
</div>
<p>p1</p>
<p>p4</p>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>

    $(".outer p").css("color","red")   //后代
    //$(".outer>p").css("color","red")     //子代
    //$(".outer+p").css("color","red")       //毗邻
    //$(".outer~p").css("color","red")       //普通兄弟,向下不向上

</script>

</body>
</html>

层级选择器示例
层级选择器示例
 属性选择器
$('[id]')    //属性名
$('[id="div1"]')    //一整条属性
$('["bob="man"][id]')    //多条属性
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div bob="man">bob1</div>
<div bob="man" class="c1">bob2</div>
<div bob="man2">bob3</div>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>
    // ==================属性选择器

     $("[bob]").css("color","red")
//     $("[bob='man']").css("color","red")
//     $("[bob='man'][class]").css("color","red")

</script>

</body>
</html>

属性选择器示例
属性选择器示例
 表单选择器

只针对input表单,按照类型选择

$("[type='text']")
$(":text")    //简写     
$("input:checked").hide();    //hide()方法,隐藏元素
//以上选择器只适用于input标签,input:checked只能用于单选框和复选框
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<input type="text">
<input type="text">
<input type="text" >
<input type="checkbox"  checked="checked">

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>
    //  针对input表单
    $("[type='text']").css("border","1px solid red");
    $("input:checked").hide();
    //$(":text").css("border","1px solid green");

</script>

</body>
</html>

表单选择器示例
表单选择器示例

 表单属性选择器

:enabled
:disabled
:checked
:selected
body>

<form>
    <input type="checkbox" value="123" checked>
    <input type="checkbox" value="456" checked>


  <select>
      <option value="1">Flowers</option>
      <option value="2" selected="selected">Gardens</option>
      <option value="3" selected="selected">Trees</option>
      <option value="3" selected="selected">Trees</option>
  </select>
</form>


<script src="jquery.min.js"></script>
<script>
    // console.log($("input:checked").length);     // 2

    // console.log($("option:selected").length);   // 只能默认选中一个,所以只能lenth:1

    $("input:checked").each(function(){

        console.log($(this).val())
    })

</script>


</body>

表单属性选择器示例
表单属性选择器示例

 筛选器

 基本筛选器
$("li:first")      //按索引取第一个
$("li:eq(2)")    //按索引取第三个
$("li:even")     //取偶数
$("li:odd")     //取偶数
$("li:gt(1)")    //大于索引1,也有lt小于
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul class="box">
    <li class="item">111</li>
    <li class="item">222</li>
    <li class="item">333</li>
    <li class="item">444</li>
    <li class="item">555</li>
    <li class="item">666</li>
</ul>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>

//    $("li:first").css("color","red");
//    $("li:odd").css("color","red");
//    $("li:even").css("color","red");
//    $("li:eq(3)").css("color","red");
//    $("li:gt(1)").css("color","red");
    $("li:gt(1):lt(3)").css("color","red"); //链式语法,前面的大于1的结果然后在结果上再小于3

</script>

</body>
</html>

基本删选器示例
基本删选器示例
 过滤筛选器

推荐的筛选器写法,可用于传变量

$("li").eq(2)  
$("li").first()  
$("ul li").hasClass("test")  //判断是否存在class为test的元素
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul class="box">
    <li class="item">111</li>
    <li class="item">222</li>
    <li class="item">333</li>
    <li class="item">444</li>
    <li class="item">555</li>
    <li class="item">666</li>
</ul>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>
    var $x=3
    $("ul li").eq($x).css("color","red");
    console.log($("ul li").hasClass('item'));
</script>
</body>
</html>

过滤删选器示例
过滤删选器示例
 查找筛选器
//查找子标签:
$("div").children(".test")
$("div").find(".test")      
                               
//向下查找兄弟标签:    
$(".test").next()               
$(".test").nextAll()    
$(".test").nextUntil()
                           
//向上查找兄弟标签:    
$("div").prev()                  
$("div").prevAll()       
$("div").prevUntil()   

//查找所有兄弟标签:    
$("div").siblings()  
              
//查找父标签:         
$(".test").parent()              
$(".test").parents()     
$(".test").parentUntil() 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div class="c1">
    <div class="c3">
        DIV
        <div class="c4">
            <p class="p1">P1</p>
        </div>
    </div>
    <p>P2</p>
</div>

<div class="c1">
    <p class="item" id="d1">p3</p>
    <p class="item">p4</p>
    <p class="item">p5</p>
    <p class="item" id="d4">p6</p>
    <p class="item">p7</p>
</div>

<div id="c1" egon="123"></div>



<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>
    // jquery支持链式操作:前一段的结果给后一段执行

    //查找子标签
    //$("div").children(".item").css("color","red") //所有后代
    //$("div").find(".p1  ").css("color","red") //所有子代

    //==========================================找兄弟标签
    //next
    //$("#d1").next().css("color","red").next().css("color","green");
    //$("#d1").nextAll().css("color","red");
    //$("#d1").nextUntil("#d4").css("color","red");   //结果为p4、p5

    //prev同next,只是方向为向上


    //siblings
    //$("#d4").siblings().css("color","red");


    //===============================================找父标签
    //console.log($(".p1").parent().parent().attr("class"))
    //console.log($(".p1").parents().attr("class"))
    //$(".p1").parents().css("color","red")
    //$(".p1").parentsUntil(".c1").css("border","1px solid red")
    
</script>

</body>
</html>

查找筛选器示例
查找筛选器示例

  操作元素(属性、css、文档处理)

 事件

 页面载入

语法

ready(fn)  // 当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。
$(document).ready(function(){}) -----------> $(function(){})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../2017.8.14/jquery-3.2.1.js"></script>
</head>
<body>
<script>
    $(document).ready(function(){
        alert(123)
    })
//    $(function(){
//        alert(123)
//    })
    
</script>

</body>
</html>

页面载入示例
页面载入示例
事件绑定

语法

jquery_obj集合.事件(function(){})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul id="box">
    <li class="item">111</li>
    <li class="item">222</li>
    <li class="item">333</li>
    <li class="item">444</li>
    <li class="item">555</li>
</ul>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>

     $(".item").click(function () {
         alert($(this).index())
     });
    
</script>
</body>
</html>

事件绑定示例
事件绑定示例

补充

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul id="box">
    <li class="item">111</li>
    <li class="item">222</li>
    <li class="item">333</li>
    <li class="item">444</li>
    <li class="item">555</li>
</ul>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>

//  ====================bind

    $(".item").bind("click",function () {
        alert(123)
    });

</script>
</body>
</html>

绑定方法补充,现在jq版本等于已经被遗弃了
绑定方法补充
 事件委派

在原有标签基础上,后边添加的标签,继承原有标签的事件

语法

jquery_obj集合.on(事件,[selector],[data],function)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul id="box">
    <li class="item">111</li>
    <li class="item">222</li>
    <li class="item">333</li>
    <li class="item">444</li>
    <li class="item">555</li>
</ul>

<button class="add">ADD</button>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>
     $(".item").click(function () {
         alert($(this).index())
     });
    
    //  on方法
    $("#box").on("click",".item",function () {
    //  alert($(this).html());
        alert($(this).index());
    });

    //添加一个标签,点击该标签显示索引
     $(".add").click(function () {
         $("#box").append("<div class='item'>666</div>")
     });

</script>
</body>
</html>

事件委派示例
事件委派示例
 事件委派清除

清除标签绑定的事件

语法

jquery_obj集合.off(事件)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul id="box">
    <li class="item">111</li>
    <li class="item">222</li>
    <li class="item">333</li>
    <li class="item">444</li>
    <li class="item">555</li>
</ul>

<button class="add">ADD</button>
<button class="releave">off</button>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>

    $("#box").on("click",".item",function () {
        alert($(this).index());
    });

     $(".add").click(function () {
         $("#box").append("<div class='item'>666</div>")
     });

    $(".releave").click(function () {
        //绑定给那个对象就解除哪个对象的事件委派
       $("#box").off("click")
    });

</script>
</body>
</html>

事件委派清除
事件委派清除
 事件切换

hover事件

一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态。

over:鼠标移到元素上要触发的函数

out:鼠标移出元素要触发的函数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .test{
             200px;
            height: 200px;
            background-color: wheat;

        }
    </style>
</head>
<body>


<div class="test"></div>


<script src="../2017.8.14/jquery-3.2.1.js"></script>
<script>
    function enter(){
        alert("enter")
    }
    function out(){
        alert("out")
    }
    $(".test").hover(enter,out)


//    $(".test").mouseenter(function(){
//        alert("enter")
//    });

//    $(".test").mouseleave(function(){
//        alert("leave")
//    });

</script>
</body>

</html>

hover示例
hover示例

 属性操作

 CSS类操作

语法

$("").addClass(class|fn)
$("").removeClass([class|fn])
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tab</title>

    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .tab_outer{
            margin: 20px auto;
             60%;
        }
        .menu{
            background-color: #cccccc;
            line-height: 40px;
            text-align: center;
        }
        .menu li{
            display: inline-block;
            margin-left: 14px;
            padding:5px 20px;

        }
        .menu a{
            border-right: 1px solid red;
            padding: 11px;
        }
        .content{
            background-color: tan;
            border: 1px solid green;
            height: 300px;

        }
        .hide{
            display: none;
        }

        .current{
            background-color: #2868c8;
            color: white;
            border-top: solid 2px rebeccapurple;
        }
    </style>
</head>
<body>
      <div class="tab_outer">
          <ul class="menu">
              <li relation="c1" class="current">菜单一</li>
              <li relation="c2" >菜单二</li>
              <li relation="c3">菜单三</li>
          </ul>
          <div class="content">
              <div id="c1">内容一</div>
              <div id="c2" class="hide">内容二</div>
              <div id="c3" class="hide">内容三</div>
          </div>

      </div>
</body>

<script src="../2017.8.14/jquery-3.2.1.js"></script>
    <script>
          $(".menu li").click(function(){
               var index=$(this).attr("relation");
               $("#"+index).removeClass("hide").siblings().addClass("hide");
               $(this).addClass("current").siblings().removeClass("current");
          });
    </script>
</html>

CSS类操作示例-tab
CSS类操作示例-tab
 属性操作

语法

$("").attr();
$("").removeAttr();
$("").prop();
$("").removeProp();
<input id="chk1" type="checkbox" />是否可见
<input id="chk2" type="checkbox" checked="checked" />是否可见



<script>

//对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
//对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
//像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此
//需要使用prop方法去操作才能获得正确的结果。


//    $("#chk1").attr("checked")
//    undefined
//    $("#chk1").prop("checked")
//    false

//  ---------手动选中的时候attr()获得到没有意义的undefined-----------
//    $("#chk1").attr("checked")
//    undefined
//    $("#chk1").prop("checked")
//    true

    console.log($("#chk1").prop("checked"));//false
    console.log($("#chk2").prop("checked"));//true
    console.log($("#chk1").attr("checked"));//undefined
    console.log($("#chk2").attr("checked"));//checked
</script>

属性操作示例
属性操作示例
HTML/Text/value

语法

$("").html([val|fn])
$("").text([val|fn])
$("").val([val|fn|arr])
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day48/jquery-3.2.1.js'></script>
</head>
<body>

<div class="c1">
    <p>PPP</p>
</div>

<input type="text" value="123">

<div value="123"></div>

<script>

    // 取值操作
    // html是标签操作、text是文本操作
    // console.log($(".c1").html());
    // console.log($(".c1").text());

    //赋值操作
    // $(".c1").html("<a href=''>click</a>")
    // $(".c1").text("<a href=''>click</a>")


    // 对value属性取值和赋值操作
    //console.log($(":text").val());    //取值
    //$(":text").val(456);  //赋值

    // 注意:value属性必须是固有属性
    console.log($("div").val())   // 取不到value属性的值
    
</script>
</body>
</html>

HTML/Text/value示例
HTML/Text/value示例

 each循环

例如:

$("p").css("color","red") 

运行的过程中内部是一个循环,但是如果该循环中的某一个元素需要特殊处理的时候,就不能使用内部的默认循环了 

 方式一
var arr=[111,222,333];
var  obj={"name":"egon","age":123};
$.each(arr,function (i,j) {
      console.log(i);   //索引
      console.log(j);   //值
});
 方式二
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../2017.8.14/jquery-3.2.1.js'></script>
</head>
<body>

<p>111</p>
<p>222</p>
<p>333</p>
<p>444</p>

<script>
    $("p").each(function () {
      console.log($(this).index());    // $(this) ----=>代指当前循环到的标签对象
    })

    // 注意:下面的this指代$("p")集合中的一个元素
    // $("p").click(function () {
    //    console.log($(this).index())
    // });

</script>
</body>
</html>
 each扩展
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../2017.8.14/jquery-3.2.1.js'></script>
</head>
<body>

<p>111</p>
<p>222</p>
<p>333</p>
<p>444</p>

<script>
  
    // 示例1
    //function f(){
    //    for(var i=0;i<4;i++){
    //        if (i==2){
    //            return
    //        }
    //        console.log(i)
    //    }
    //}
    //f();

    //示例2
    //li=[11,22,33,44];
    //$.each(li,function(i,v){
    //
    //    if (v==33){
    //            return false;   //  ===试一试 return false会怎样?
    //        }
    //    console.log(v)
    //});
    // each的参数function内如果出现return  结束当次循环,类似于continue;
    //             如果出现return False,结束的是整个each循环,类似break。

</script>
</body>
</html>

 文档节点处理

 创建一个标签对象

语法

$("<标签名>")
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='jquery-3.2.1.js'></script>
</head>
<body>
<div class="outer">
</div>
<script>
    var $ele=$("<p>");    // <p></p>
    $ele.text("hello world");  // <p>hello world</p>
</script>
</body>
</html>

创建节点示例
创建节点示例
 标签内部插入标签对象

语法

$("").append(content|fn)      ----->$("p").append("<b>Hello</b>");
$("").appendTo(content)       ----->$("p").appendTo("div");
$("").prepend(content|fn)     ----->$("p").prepend("<b>Hello</b>");
$("").prependTo(content)      ----->$("p").prependTo("#foo");

注意:可以使用function添加内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>
<div class="outer">
    <p>abc</p>
</div>

<script>
    //内部插入
    var $ele=$("<p>");    // <p></p>
    $ele.text("hello world");  // <p>hello world</p>

    // 追加到最后的位置
    $(".outer").append($ele);
    //     $ele.appendTo(".outer");

    // 添加到最上面的位置
    var $ele2=$("<p>")
    $ele2.text("thank you");
    $(".outer").prepend($ele2);
    //    $ele.prependTo(".outer")
</script>
</body>
</html>

内部插入示例
内部插入示例
 标签外部插入标签对象

语法

$("").after(content|fn)       ----->$("p").after("<b>Hello</b>");
$("").before(content|fn)      ----->$("p").before("<b>Hello</b>");
$("").insertAfter(content)    ----->$("p").insertAfter("#foo");
$("").insertBefore(content)   ----->$("p").insertBefore("#foo");

注意:可以使用function添加内容 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>

<div class="outer">
    <p>abc</p>
</div>

<script>
    //内部插入
    var $ele=$("<p>"); 
    $ele.text("hello world");  
    // 在标签后添加
    $(".outer").after($ele)     //
    //$($ele).insertAfter(".outer")

    //在标签前添加
    var $ele2=$("<p>")
    $ele2.text("thank you");
    $(".outer").before($ele2)     
    //$($ele2).insertBefore(".outer")

</script>
</body>
</html>

外部插入示例
外部插入示例
 删除节点

语法

$("").empty()    //内容删除
$("").remove([expr]    //整个标签删除
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>

<div class="outer">
    <p>abc</p>
</div>

<script>

    // 删除节点,连标签清除
    // $(".outer").remove()
    // 清空节点,内容清除
    $(".outer").empty();

</script>
</body>
</html>

删除节点示例
删除节点示例
 替换节点

语法

$("").replaceWith(content|fn)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>

<div class="outer">
    <p>abc</p>
</div>

<script>

    // 替换节点
    var $ele=$("<p>"); 
    $ele.text("hello world"); 
    $(".outer p").replaceWith($ele);

</script>
</body>
</html>

替换节点示例
替换节点示例
 拷贝节点

语法

 $("").clone([Even[,deepEven]])
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>

<div class="outer">
    <p>abc</p>
</div>

<script>

    // 拷贝节点
    var $outer=$(".outer").clone();
    $(".outer").after($outer)

</script>
</body>
</html>

拷贝节点示例
拷贝节点示例

 动画效果

 显示与隐藏
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>
<p>hello world</p>
<img src="http://img1.imgtn.bdimg.com/it/u=1020860587,2909379450&fm=26&gp=0.jpg" alt="">
<hr>
<button class="hides">hide</button>
<button class="shows">show</button>
<button class="toggle">toggle</button>

<script>
    //注意:不加时间试一试
    //点击事件触发隐藏
    $(".hides").click(function () {
        $("img").hide(1000)
    });
    //点击事件触发显示
    $(".shows").click(function () {
        $("img").show(1000)
    });
    //点击事件触发显示和隐藏的切换
    $(".toggle").click(function () {
        $("img").toggle(1000)
    })
</script>
</body>
</html>

显示与隐藏示例
显示与隐藏示例
 滑动
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='jquery-3.2.1.js'></script>
</head>
<body>

<p style="background-color: #2459a2;color: white;text-align: center;line-height: 40px">hello world</p>

<img src="http://img1.imgtn.bdimg.com/it/u=1020860587,2909379450&fm=26&gp=0.jpg" alt="">
<hr>
<button class="slideUp">slideUp</button>
<button class="slideDown">slideDown</button>
<button class="slideToggle">slideToggle</button>


<script>
    //注:不加时间试一试
    //向上滑动消失
    $(".slideUp").click(function () {
        $("p").slideUp(1000);
    });
    //向下滑动出现
    $(".slideDown").click(function () {
        $("p").slideDown(1000)
    });
    //向上向下切换
    $(".slideToggle").click(function () {
        $("p").slideToggle(1000)
    })

</script>
</body>
</html>

滑动示例
滑动示例
 淡入淡出
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='jquery-3.2.1.js'></script>
</head>
<body>
<p style="background-color: #2459a2;color: white;text-align: center;line-height: 40px">hello world</p>
<img src="http://img1.imgtn.bdimg.com/it/u=1020860587,2909379450&fm=26&gp=0.jpg" alt=""><hr>
<button class="fadeIn">fadeIn</button>
<button class="fadeOut">fadeOut</button>
<button class="fadeToggle">fadeToggle</button>
<button class="fadeTo">fadeTo</button>

<script>
    //淡入
    $(".fadeIn").click(function () {
        $("img").fadeIn(2000);
    });
    //淡出
    $(".fadeOut").click(function () {
        $("img").fadeOut(2000)
    });
    //淡出淡入切换
     $(".fadeToggle").click(function () {
        $("img").fadeToggle(1000)
    })
    //淡出或淡入的程度
    $(".fadeTo").click(function(){
       $("img").fadeTo(1000,0.4);
    })
</script>
</body>
</html>

淡入和淡出示例
淡入和淡出示例
 回调函数
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='../day47/jquery-3.2.1.js'></script>

</head>
<body>
<button>hide</button>
<p>hello world</p>

<script>
    $("button").click(function(){
        $("p").hide(1000,function(){
           alert($(this).html())
        })
    })
</script>
</body>
</html>

回调函数示例
回调函数示例

 CSS操作

 CSS位置操作

语法

$("").offset([coordinates])    //元素移动,定位对象是整个页面,
$("").position()    //元素偏移(定位对象和offset不一样,是通过父亲标签定位)
$("").scrollTop([val])  //上下滚动条的值
$("").scrollLeft([val])  //左右滚动条的值

offset元素移动示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
        }
        .box1{
             200px;
            height: 200px;
            background-color: yellowgreen;
        }
        .box2{
             200px;
            height: 200px;
            background-color: rebeccapurple;
        }
    </style>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<button>change</button>

<script>
    //元素移动的是根据整个页面的距离实现的,通过改变元素的上侧、左侧和整个页面的上侧和左侧的距离改变
    $("button").click(function () {
        //.box1向下移动200px
        $(".box1").offset({left:0,top:200});
        //.box2向下移动400px,向右移动200px
        $(".box2").offset({left:200,top:400});
    })
</script>

</body>
</html>

元素移动示例
元素移动示例

position元素偏移

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>

        *{
            margin: 0;
        }
        .box1{
             200px;
            height: 200px;
            background-color: wheat;
        }
        .box2{
             200px;
            height: 200px;
            background-color: green;
        }

        .outer{
            position: relative;
        }
    </style>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>

<div class="box1"></div>
<div class="outer">
    <div class="box2"></div>
</div>
<button>change</button>

<script>
    $("button").click(function () {
        alert("left"+$(".box1").position().left + "top" + $(".box1").position().top)
        alert("left"+$(".box2").position().left + "top" + $(".box2").position().top)
</script>
</body>
</html>

position示例
position示例

scrollTop滚动条

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{
             100%;
            height: 2000px;
            background-color: wheat;
        }

        #returnTop{
             70px;
            height: 40px;
            background-color: #2459a2;
            color: white;
            font-weight: 800;
            text-align: center;
            line-height: 40px;
            position: fixed;
            bottom: 20px;
            right: 20px;
            display: none;
        }
    </style>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>

<div class="box"></div>
<div id="returnTop">TOP</div>
<script>
    //scroll事件,滚动进度条时候
    $(window).scroll(function () {
        console.log($(window).scrollTop());
        if($(window).scrollTop()>200){
            $("#returnTop").show();

        }
        else {
            $("#returnTop").hide();
        }
    });
    //scrollTop括号里加数字,表示将滚动条位置滚至数字像素,不加数字表示取值
    $("#returnTop").click(function () {
        $(window).scrollTop(0)
    })
</script>
</body>
</html>

scrollTop示例
scrollTop示例
 尺寸操作

语法

$("").height([val|fn])
$("").width([val|fn])
$("").innerHeight()
$("").innerWidth()
$("").outerHeight([soptions])
$("").outerWidth([options])
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .box{
             200px;
            height: 200px;
            padding: 50px;
            border: 10px red solid;
            background-color: green;
            margin: 20px;
        }
    </style>
    <script src='../day47/jquery-3.2.1.js'></script>
</head>
<body>


<div class="box">DIV</div>


<script>
     console.log($(".box").height());  //内容区高度200
     console.log($(".box").width());   //内容区宽度200

     console.log($(".box").innerHeight());   //内容区+padding区高度300
     console.log($(".box").innerWidth());    //内容区+padding区宽度300

     console.log($(".box").outerHeight());   //内容区+padding区+border边框区高度 320
     console.log($(".box").outerWidth());    //内容区+padding区+border边框区宽度 320

     console.log($(".box").outerHeight(true));   ///内容区+padding区+border边框区宽度+margin区高度 320
     console.log($(".box").outerWidth(true));    ///内容区+padding区+border边框区宽度+margin区高度 320

</script>
</body>
</html>

尺寸操作示例
尺寸操作示例
原文地址:https://www.cnblogs.com/lidagen/p/7444743.html