20190716-jQuery实例

20190716-jQuery实例

                                        ——Javee

案例六:

<!DOCTYPE html>
<html>
     <head>
         <meta charset="UTF-8">
         <title></title>
         <style>
              *{
                  margin: 0;
                  padding: 0;
                  font-size: 12px;
              }
              #win{
                  width: 600px;
                  margin: auto;
                  text-align: center;
              }
              #btn{
                  margin-top: 10px;
                  color: #0088FF;
                  width: 600px;
                  float: left;
                  text-align: center;
                  box-sizing: border-box;
                  
              }
              #btn>div{
                  width: 150px;
                  border: 1px solid #CCC;
                  margin: auto;
                  padding: 5px 0;
                  cursor: pointer;
                  user-select: none;
                  box-sizing: border-box;
              }
              #btn>div>div{
                  border: 1px solid orange;
                  display: inline-block;
                  margin-right: 7px;
              }
              
              ul>li{
                  list-style-type: none;
                  width: 200px;
                  float: left;
                  margin-top: 5px;
                  cursor: pointer;
              }
              
              ul>li>span{
                  color: #0088FF;
              }
              
              ul>li:nth-of-type(n+7):not(:last-of-type){
                  display: none;
              }
              .orange>span{
                  color: orange;
              }
              
              .triangle-bottom {
                  display: block;
                  width: 0;
                  height: 0;
                  border-left: 4px solid transparent;
                  border-right: 4px solid transparent;
                  border-top: 4px solid orange;
                  margin: 3px 1px;
              }
              .triangle-top {
                  display: block;
                  width: 0;
                  height: 0;
                  border-left: 4px solid transparent;
                  border-right: 4px solid transparent;
                  border-bottom: 4px solid orange;
                  margin: 3px 1px;
              }
              
         </style>
         <script  src="../js/jquery-3.4.1.min.js"></script>
         <script>
              $(function(){
                  $("#btn>div").click(function(){
                       var liList =  $("ul>li:gt(5)").not($("ul>li:last-child"));
                       liList.toggle();                
                       

                      $("ul>li").filter(function(index,obj){
                      if(index == 0 || index == 3 || index  == 10) return obj;
                     }).toggleClass("orange");
                       
                       $("#btn>div>div>i").toggleClass("triangle-bottom");
                       $("#btn>div>div>i").toggleClass("triangle-top");
                       
                       if(liList.is(":visible")){                        
                            $("#btn>div>span").text("显示精简品牌");
                            
                       }else{
                            $("#btn>div>span").text("显示全部品牌");
                       }
                       
                  });
              });
         </script>
     </head>
     <body>
         <div id="win">
              <ul>
                  <li><span>佳能</span><i>(30440)</i></li>
                  <li><span>索尼</span><i>(27220)</i></li>
                  <li><span>三星</span><i>(20808)</i></li>
                  <li><span>尼康</span><i>(17821)</i></li>
                  <li><span>松下</span><i>(12289)</i></li>
                  <li><span>卡西欧</span><i>(8242)</i></li>
                  <li><span>富士</span><i>(14894)</i></li>
                  <li><span>柯达</span><i>(9520)</i></li>
                  <li><span>宾博</span><i>(2195)</i></li>
                  <li><span>理光</span><i>(4114)</i></li>
                  <li><span>奥林巴斯</span><i>(12205)</i></li>
                  <li><span>明基</span><i>(1466)</i></li>
                  <li><span>爱国者</span><i>(3091)</i></li>
                  <li><span>其他品牌相机</span><i>(7275)</i></li>
              </ul>
              <div id="btn"><div><div><i  class="triangle-bottom"></i></div><span>显示全部品牌</span></div></div>
         </div>
     </body>
</html>


案例7:(天猫首页图片轮播效果)

<!DOCTYPE html>
<html>
     <head>
          <meta charset="UTF-8">
          <title></title>
          <style>
               * {
                    margin: 0px;
                    padding: 0px;
               }
               
               li {
                    list-style-type: none;
                    height: 500px;
               }
               
               #win {
                    margin: auto;
                    padding: 0px;
                    width: 1234px;
               }
               
               #win>li:not(:first-of-type) {
                    display: none;
               }
               
               .bg1 {
                    background: #E8E8E8 url("img/1.jpg")  no-repeat center;
               }
               
               .bg2 {
                    background: #E8E8E8 url("img/2.jpg")  no-repeat center;
               }
               
               .bg3 {
                    background: #FEE4E3 url("img/3.jpg")  no-repeat center;
               }
               
               .bg4 {
                    background: #e8e8e8 url("img/4.jpg")  no-repeat center;
               }
               
               .bg5 {
                    background: #d5FFFe url("img/5.jpg")  no-repeat center;
               }
               
               .bg6 {
                    background: #e8e8e8 url("img/6.jpg")  no-repeat center;
               }
               
               #main {
                    position: relative;
               }
               
               #btns>a {}
               
               #btns {
                    width: 194px;
                    position: absolute;
                    z-index: 9999;
                    bottom: 22px;
                    left: calc(50% - 97px);
               }
               
               #btns>a {
                    float: left;
                    width: 24px;
                    height: 5px;
                    margin-left: 7px;
                    background-color: rgba(0, 0, 0, 0.4);
               }
               
               .choose {
                    background-color: rgba(255, 255, 255, 1)  !important;
               }
          </style>
          <script src="../js/jquery-3.4.1.min.js"></script>
          <script>
               $(function(){
                    var colors =  ["#E8E8E8","#E8E8E8","#FEE4E3","#E8E8E8","#d5FFFe","#E8E8E8"];
                    var nowIndex = 0;
                    var mytime;
                    var myinterval;
                    
                    $("#btns>a").mouseenter(function(){                         
                         var mythis = this;
                         if($(mythis).hasClass("choose"))  return;
                         clearTimeout(mytime);
                         clearInterval(myinterval);
                         mytime = setTimeout(function(){
                              $("#btns>a").removeClass("choose");
                              $(mythis).addClass("choose");
                              
                              var index =  $("#btns>a").index(mythis);
                              $("#win>li").hide();
                              $("#win>li").eq(index).fadeIn(500);
                              $("#main").css("backgroundColor",colors[index]);
                              nowIndex = index;
                         },300);   
                         myinterval =  setInterval(mymove,3000);
                    });
                    myinterval = setInterval(mymove,3000);
                    function mymove(){
                         nowIndex++;
                         if(nowIndex >= 6) nowIndex = 0;
                         
                         $("#btns>a").removeClass("choose");
                         $("#btns>a").eq(nowIndex).addClass("choose");
                         $("#win>li").hide();
                         $("#win>li").eq(nowIndex).fadeIn(500);
                         $("#main").css("backgroundColor",colors[nowIndex]);
                    }
               });
          </script>
     </head>
     <body>
          <div id="main" style=";background-color:#E8E8E8;  ">
               <ul id="win">
                    <li class="bg1"></li>
                    <li class="bg2"></li>
                    <li class="bg3"></li>
                    <li class="bg4"></li>
                    <li class="bg5"></li>
                    <li class="bg6"></li>
               </ul>
               <div id="btns">
                    <a class="choose"  href="javascript:;"></a>
                    <a href="javascript:;"></a>
                    <a href="javascript:;"></a>
                    <a href="javascript:;"></a>
                    <a href="javascript:;"></a>
                    <a href="javascript:;"></a>
               </div>
          </div>
     </body>
</html>

案例8:(动态滚动到顶部)

$(function(){
    $("#topbtn").click(function(){
        $("html").animate({"scrollTop":"0px"},500);
    });
});

案例9:(天猫首页右侧移入移出,提示信息淡入淡出的动画)

<!DOCTYPE html>
<html>
     <head>
           <meta charset="UTF-8">
           <title></title>
           <style>
                *{
                      margin: 0;
                      padding: 0;
                }
                #leftDiv{
                     background-color: black;
                     width: 30px;
                     height: 100%;
                     position: fixed;
                     right: 0px;
                }
                #tip{
                      width: 100px;
                      height: 35px;
                      position: absolute;   
                      right: 60px;
                      top: 100px;     
                      display: none;        
                }
                
                #rectangle{
                      width: 92px;
                      height: 35px;
                      background-color: #494949;
                      font-size: 12px;
                      color: #FFF;
                      line-height: 35px;
                      text-align: center;
                }
                #triangle{
                      width: 0;
                      height: 0;
                      border-bottom: 7px solid transparent;
                      border-top: 7px solid transparent;
                      border-left: 8px solid #494949;
                      position: absolute;
                      right: 0px;
                      top: 12px;
                }
           </style>
           <script src="../js/jquery-3.4.1.min.js"></script>
           <script>
                $(function(){
                      $("#leftDiv").hover(function(){
                           $("#tip").css("opacity","0").show().stop()
                                 .animate({"opacity":1,"right":"20px"},500);
                      },function(){
                           $("#tip").stop()
                                .animate({"opacity":0,"right":"60px"},500,function(){
                                 $(this).hide();
                           });
                      });
                });
           </script>
     </head>
     <body>
           <div id="leftDiv">
                <div id="tip">
                      <div id="rectangle">会员权益</div>
                      <div id="triangle"></div>
                </div>
           </div>
     </body>
</html>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery是一种JavaScript库,ajax是一种能够向服务器端发送和接收请求的技术。在Web开发中,ajax技术可以让页面在不刷新的情况下向服务器请求数据,使得页面更加丰富和动态。 下面就是一个jQuery ajax请求的实例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jQuery Ajax Request</title> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <!--需要引入jQuery库--> <script> $(document).ready(function() { $("#btn").click(function() { //当按钮被点击时 $.ajax({ url: "data.php", //请求地址 data: {"name":"Tom", "age":18}, //请求参数 type: "post", //请求方式 dataType: "json", //返回值类型 success: function(result) { //请求成功时的回调函数 $("#result").html(result.name + "今年" + result.age + "岁"); //将返回值显示在页面上 }, error: function() { //请求失败时的回调函数 alert("请求失败"); } }); }); }); </script> </head> <body> <button id="btn">请求数据</button><br><br> <div id="result"></div> </body> </html> ``` 在这个实例中,我们使用了jQuery的ajax函数,这个函数有很多参数,其中一些常用的参数包括: - url:请求地址; - data:请求参数; - type:请求方式(常用的有get和post); - dataType:返回值类型(常用的有json和html); - success:请求成功时的回调函数; - error:请求失败时的回调函数。 在按钮被点击时,我们向服务器请求数据,服务器通过data.php来处理请求,并返回一个json格式的数据: ```php <?php $name = $_POST["name"]; $age = $_POST["age"]; $result = array("name" => $name, "age" => $age); echo json_encode($result); ?> ``` 当请求成功时,我们将返回的数据解析出来,并将其显示在页面上。如果请求失败,我们就弹出一个提示框。通过这个实例,我们可以学习到如何使用jQuery来进行ajax请求,以及如何处理请求成功和失败的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值