1.swiper


轮播的使用

  1. <link href="https://cdn.bootcss.com/Swiper/4.5.0/css/swiper.css" rel="stylesheet">
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.3.5/js/swiper.js"></script>
  3. <script src="swiper/swiper.js"></script>
  1. window.onload = function () {
  2. var mySwiper = new Swiper('.swiper-container', {
  3. // direction: 'horizonal',
  4. loop: true,
  5. autoplay: {
  6. delay: 2000,
  7. stopOnLastSlide: false,
  8. disableOnInteraction: false,
  9. },
  10. // 如果需要分页器
  11. pagination: {
  12. el: '.swiper-pagination',
  13. clickable: true
  14. },
  15. // 如果需要前进后退按钮
  16. navigation: {
  17. nextEl: '.swiper-button-next',
  18. prevEl: '.swiper-button-prev',
  19. }
  20. });
  21. };

2.事件流

描述的是从页面中接收事件的顺序。

1.事件冒泡: 阻止事件冒泡:event.stopPropagation

  1. 父元素和子元素绑定了相同的事件,子元素的事件执行,父元素的事件也会触发
  1. <style>
  2. #parent{
  3. width:200px;
  4. height:200px;
  5. background: red;
  6. }
  7. #child{
  8. width:100px;
  9. height:100px;
  10. background: yellow;
  11. margin:50px
  12. }
  13. </style>
  14. <script>
  15. var parent=document.getElementById("parent")
  16. var child=document.getElementById("child")
  17. parent.onclick=function(event){
  18. alert("parent")
  19. event.stopPropagation()
  20. }
  21. child.onclick=function(event){
  22. alert('child')
  23. event.stopPropagation()
  24. }
  25. </script>

2.事件监听 addEventListener(event)

  1. <script>
  2. /* onclick --直接绑定的方式,只会触发一次
  3. 事件监听 addEventListener(event)
  4. */
  5. var parent=document.getElementById("parent")
  6. var child=document.getElementById("child")
  7. child.addEventListener('click',function(){
  8. console.log(1)
  9. })
  10. child.addEventListener('click',function(){
  11. console.log(2)
  12. })
  13. </script>

3.事件捕获

从不太具体的节点 最早接收事件,而最具体的节点 最后接收事件

  1. 父元素和子元素绑定了相同的事件,父元素的事件先触发,子元素的事件后触发
  2. addEventListener(event,function,boolean)
  3. true -->事件捕获
  4. false-->事件冒泡
  5. <script>
  6. var parent=document.getElementById("parent")
  7. var child=document.getElementById("child")
  8. child.addEventListener('click',function(){
  9. alert("child")
  10. },true)
  11. parent.addEventListener('click',function(){
  12. alert("parent")
  13. },true)
  14. </script>

3.array


  1. <script>
  2. /* 给数组添加一个值 */
  3. var arr=[1,2,3,4];
  4. /*
  5. push -->给数组尾部添加值
  6. splice(startNumber,number)
  7. */
  8. arr.push(5);
  9. arr.splice(1,1)
  10. console.log(arr)
  11. </script>

4.节点


  1. <div id="parent">
  2. hello world
  3. <p id="first">good</p>
  4. <p>good</p>
  5. <p>good</p>
  6. </div>
  7. <script>
  8. /*
  9. childNodes -->获取子节点 不会区分文本和元素节点
  10. children -->获取元素节点
  11. nextSibling -->获取下一个兄弟节点
  12. nextElementSibling -->获取下个兄弟元素节点
  13. */
  14. var parent=document.getElementById("parent")
  15. var childs=parent.children;
  16. var child=parent.childNodes;
  17. var first=document.getElementById("first")
  18. console.log(childs)
  19. console.log(child)
  20. console.log(first.nextElementSibling)
  21. </script>

捕获.PNG

5.补充


  1. offsetParent //获取给了定位元素的父级
  2. offsetLeft //返回元素的相对定位父元素水平偏移位置。 返回number
  3. offsetTop //返回元素的相对定位父元素水平垂直偏移位置。
  4. offsetWidth //返回元素的宽度 -- 包含width,padding,border
  5. offsetHeight //返回元素的高度
  6. 可视区域的宽度和高度 clientWidth clientHeight
  7. 获取滚动区域的宽高 scrollWidth scrollHeight

6.操作元素属性


  1. <p id="p" class="one">hello world</p>
  2. <script>
  3. /* 操作属性
  4. setAttribute
  5. getAttribute
  6. removeAttribute
  7. */
  8. var p =document.getElementById("p");
  9. p.onclick=function(){
  10. this.setAttribute("style","display:none")
  11. }
  12. console.log(p.getAttribute("class"))
  13. console.log(p.removeAttribute("class"))
  14. </script>

7.文档碎片


  1. <ul id="parent">
  2. </ul>
  3. <script>
  4. var parent=document.getElementById("parent")
  5. var frag=document.createDocumentFragment();
  6. for(var i=0;i<=10;i++){
  7. var li=document.createElement("li");
  8. li.innerHTML=i;
  9. frag.appendChild(li)
  10. }
  11. parent.appendChild(frag)
  12. </script>

8.批量操作css


  1. <p id="test">hello world</p>
  2. <script>
  3. /*
  4. cssText -->批量操作css
  5. length
  6. getPropertyValue()
  7. item() -->返回对应位置的css属性名
  8. removeProperty()
  9. setProperty
  10. */
  11. var test=document.getElementById("test");
  12. test.style.cssText="color:red;background-color:#333"
  13. console.log(test.style.getPropertyValue("color"))
  14. console.log(test.style.item(0))
  15. test.onclick=function(){
  16. this.style.removeProperty("color")
  17. }
  18. </script>

9.try-throw


  1. <input type="text" id="input">
  2. <button id="btn">btn</button>
  3. <p id="msg"></p>
  4. <script>
  5. /* try-catch-throw */
  6. var btn=document.getElementById("btn")
  7. var input =document.getElementById("input")
  8. var msg=document.getElementById("msg")
  9. btn.onclick=function(){
  10. try{
  11. var value=input.value;
  12. if(value==""){
  13. /* throw可以自定义错误 */
  14. throw"输入的值不能为空"
  15. }
  16. }catch(err){
  17. alert(err)
  18. }
  19. }
  20. </script>