当前位置: 首页 > news >正文

jQuery【回到顶部、Swiper轮播图、立即执行函数、链式调用、参数重载、jQuery扩展】(六)-全面详解(学习总结---从入门到深化)

目录

回到顶部

 Swiper轮播图

jQuery源码_立即执行函数

jQuery源码_链式调用

jQuery源码_参数重载

jQuery扩展


回到顶部

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="./js/jquery-3.6.0.min.js"></script><style>div {height: 500px;}#btn {width: 35px;height: 45px;position: fixed;right: 50px;bottom: 50px;background: #333;text-decoration: none;color: #fff;text-align: center;padding: 10px;display: none;}</style>
</head>
<body><div>内容1</div><div>内容2</div><div>内容3</div><div>内容4</div><div>内容5</div><div>内容6</div><!-- 让a标签失去href属性的效果 --><a href="javaScript:void(0)" id="btn">回到顶部</a><script>//当滚动条的位置处于距顶部50像素以下时,跳转链接出现,否则消失$(function () {$(window).scroll(function () {if ($(window).scrollTop() > 500) {$("#btn").fadeIn(200);} else {$("#btn").fadeOut(200);}});//当点击跳转链接后,回到页面顶部位置$("#btn").click(function () {$('body,html').animate({scrollTop: 0},500);});});</script>
</body>
</html>

 Swiper轮播图

Swiper 使用方法
1.首先加载插件,需要用到的文件有swiper-bundle.min.js和 swiper-bundle.min.css文件
 

<!DOCTYPE html>
<html>
<head>...<link rel="stylesheet" href="dist/css/swiper-bundle.min.css">
</head>
<body>...<script src="dist/js/swiper-bundle.min.js"></script>...
</body>
</html>

2.添加HTML内容。Swiper7的默认容器是'.swiper',Swiper6之前是'.swiper-container'。
 

<div class="swiper"><div class="swiper-wrapper"><div class="swiper-slide">Slide1</div><div class="swiper-slide">Slide2</div><div class="swiper-slide">Slide3</div></div><!-- 如果需要分页器 --><div class="swiper-pagination"></div><!-- 如果需要导航按钮 --><div class="swiper-button-prev"></div><div class="swiper-button-next"></div><!-- 如果需要滚动条 --><div class="swiper-scrollbar"></div>
</div>

 3.你可能想要给Swiper定义一个大小,当然不要也行

.swiper {width: 600px;height: 300px;
}  

4.初始化Swiper

<script>        var mySwiper = new Swiper ('.swiper', {direction: 'vertical', // 垂直切换选项loop: true, // 循环模式选项// 如果需要分页器pagination: {el: '.swiper-pagination',},// 如果需要前进后退按钮navigation: {nextEl: '.swiper-button-next',prevEl: '.swiper-button-prev',},// 如果需要滚动条scrollbar: {el: '.swiper-scrollbar',},})        
</script>

jQuery源码_立即执行函数

jQuery有很多设计是非常优秀的

The Write Less,Do More(写更少,做更多),无疑是jQuery的核心理念,简洁的API、优雅的链式、强大查询与便捷的操作
 

立即执行函数解决了命名冲突问题
 

(function(global, factory) {factory(global);
}(window, function(window, noGlobal) {var jQuery = function( selector, context) {return new jQuery.fn.init( selector,context );};jQuery.fn = jQuery.prototype = {};// code ...return jQuery;
}));

再简化

(function(window, noGlobal){var jQuery = function() {};//code ...
})(window, noGlobal);

jQuery源码_链式调用

链式调用可以让代码变得更加的简洁和优雅

$(this).parent().siblings().find(".content").slideUp();

 如何做到链式调用呢

var test = {a:function(){console.log('a');return this;},b:function(){console.log('b');return this;},c:function(){console.log('c');return this}
}
test.a().b().c();

jQuery源码_参数重载

jquery有个特别的设计就是参数重载
 

$('.test')
$(this)
$(function(){...})

再简化

function test(){}
test(100)
test("Hello")
test(function(){})

$() 就是一个函数,参数不同,就涉及到了函数的重载
 

function test(args){if(typeof args === "number"){console.log("数字");}if(typeof args === "string"){console.log("字符串");}if(typeof args === "function"){console.log("函数");}
}
test(100)
test("Hello")
test(function(){})

jQuery扩展

jQuery.fn.extend() 或者 $.fn.extend() 函数为 jQuery 扩展一个或多个实例属性和方法
 

扩展一个print()方法,获得当前元素的内容

$.fn.extend({print:function(){console.log($(this).html())}
})
$("p").print();

 扩展getMax()方法,获取两个数字中的最大值,使用 $ 调用

$.extend({getMax: function (x, y) {return x > y ? x : y}
})
alert($.getMax(3, 1))

http://www.lryc.cn/news/241096.html

相关文章:

  • day60
  • thingsboard的WebSocket API的使用
  • An issue was found when checking AAR metadata
  • 搭建线上jvm监控
  • 【计算机网络笔记】数据链路层概述
  • vscode-insiders Remote-SSH XHR failed无法访问远程服务器
  • Ubuntu开机显示No bootable devices found
  • 设计模式——行为型模式(二)
  • SpringBoot中企业微信的API调用
  • [前端] V8引擎编译原理
  • 使用Pytorch实现linear_regression
  • 网络安全等级保护收费标准?
  • 16 Go的反射
  • SQL Server 百万数据查询优化技巧三十则
  • list转map(根据某个或多个属性分组)
  • 常见树种(贵州省):012茶、花椒、八角、肉桂、杜仲、厚朴、枸杞、忍冬
  • 千云物流 - 使用k8s负载均衡openelb
  • C语言之字符串函数
  • python中一个文件(A.py)怎么调用另一个文件(B.py)中定义的类AA详解和示例
  • spark shuffle 剖析
  • C语言之认识柔性数组(flexible array)
  • 【MATLAB基础绘图第17棒】绘制玫瑰图
  • Qt 基于海康相机的视频绘图
  • FlinkCDC实现主数据与各业务系统数据的一致性(瀚高、TIDB)
  • JSP:Servlet
  • react中的state
  • VR全景航拍要注意什么,航拍图片如何处理
  • Spark---集群搭建
  • Linux上通过SSL/TLS和start tls连接到LDAP服务器
  • 【华为OD题库-034】字符串化繁为简-java