JavaScript基础视频教程总结(111-120章)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>111-120章总结</title>
  6. </head>
  7. <body>
  8. <pre>
  9. 111. 事件对象
  10. - 当事件的响应函数被触发时,浏览器每次都会将一个事件对象作为实参传递进响应函数,
  11. 在事件对象中封装了当前事件相关的一切信息,比如:鼠标的坐标 键盘哪个按键被按下 鼠标滚轮滚动的方向。。。
  12. </pre>
  13. <style type="text/css">
  14. #areaDiv {
  15. border: 1px solid black;
  16. width: 300px;
  17. height: 50px;
  18. margin-bottom: 10px;
  19. }
  20. #showMsg {
  21. border: 1px solid black;
  22. width: 300px;
  23. height: 20px;
  24. }
  25. </style>
  26. <div id="areaDiv"></div>
  27. <div id="showMsg"></div>
  28. <script type="text/javascript">
  29. console.log("111");
  30. //当鼠标在areaDiv中移动时,在showMsg中来显示鼠标的坐标
  31. var areaDiv = document.getElementById("areaDiv")
  32. var showMsg = document.getElementById("showMsg")
  33. areaDiv.onmousemove = function(event){
  34. event = event || window.event
  35. var x = event.clientX
  36. var y = event.clientY
  37. var result = "X coords: " + x + ", Y coords: " + y
  38. showMsg.innerHTML = result
  39. }
  40. </script>
  41. <pre>
  42. 112. div跟随鼠标移动
  43. clientXclientY
  44. 用于获取鼠标在当前的可见窗口的坐标
  45. div的偏移量,是相对于整个页面的
  46. pageXpageY可以获取鼠标相对于当前页面的坐标
  47. 但是这个两个属性在IE8中不支持,所以如果需要兼容IE8,则不要使用
  48. </pre>
  49. <style type="text/css">
  50. #box1{
  51. /*开启box1的绝对定位*/
  52. position: absolute;
  53. width: 100px;
  54. height: 100px;
  55. background-color: red;
  56. }
  57. #box2{
  58. width: 100px;
  59. height: 100px;
  60. background-color: green;
  61. }
  62. </style>
  63. <div id="yibox" style="width: 2000px;height: 200px;position: relative;">
  64. <div id="box1"></div>
  65. <div id="box2"></div>
  66. </div>
  67. <script type="text/javascript">
  68. console.log("112");
  69. var yibox = document.getElementById("yibox")
  70. var box1 = document.getElementById("box1")
  71. var box2 = document.getElementById("box2")
  72. var boxWidth = box1.clientWidth / 2
  73. var boxHeight = box1.clientHeight / 2
  74. var yiboxHeight = yibox.offsetTop
  75. yibox.onmousemove = function(event){
  76. event = event || window.event
  77. var left = event.clientX
  78. var top = event.clientY
  79. // 获取滚动条滚动的距离
  80. // chrome认为浏览器的滚动条是body的,可以通过body.scrollTop来获取
  81. // 火狐等浏览器认为浏览器的滚动条是html的,
  82. var st = document.body.scrollTop || document.documentElement.scrollTop;
  83. var sl = document.body.scrollLeft || document.documentElement.scrollLeft;
  84. // 鼠标光标位置在正中间
  85. //box1.style.left = left + sl - boxWidth + "px"
  86. //box1.style.top = top + st - boxHeight - yiboxHeight + "px"
  87. box1.style.left = left + sl + "px"
  88. box1.style.top = top + st - yiboxHeight + "px"
  89. event.cancelBubble = true;
  90. }
  91. box2.onmousemove = function(event){
  92. event = event || window.event;
  93. event.cancelBubble = true;
  94. };
  95. </script>
  96. <pre>
  97. 113. 事件的冒泡
  98. 事件的冒泡(Bubble
  99. - 所谓的冒泡指的就是事件的向上传导,当后代元素上的事件被触发时,其祖先元素的相同事件也会被触发
  100. - 在开发中大部分情况冒泡都是有用的,如果不希望发生事件冒泡可以通过事件对象来取消冒泡
  101. </pre>
  102. <style type="text/css">
  103. #mbox1{
  104. width: 200px;
  105. height: 200px;
  106. background-color: yellowgreen;
  107. }
  108. #ms1{
  109. background-color: yellow;
  110. }
  111. </style>
  112. <div id="mbox1">我是box1
  113. <span id="ms1">我是span</span>
  114. </div>
  115. <script type="text/javascript">
  116. console.log("113");
  117. var mbox1 = document.getElementById("mbox1")
  118. var ms1 = document.getElementById("ms1")
  119. // ms1绑定一个单击响应函数
  120. ms1.onclick = function(event){
  121. event = event || window.event
  122. alert("我是span的单击响应函数")
  123. //可以将事件对象的cancelBubble设置为true,即可取消冒泡
  124. event.cancelBubble = true
  125. }
  126. // mbox1绑定一个单击响应函数
  127. mbox1.onclick = function(event){
  128. event = event || window.event
  129. alert("我是div的单击响应函数")
  130. event.cancelBubble = true
  131. }
  132. //body绑定一个单击响应函数
  133. /*document.body.onclick = function(){
  134. alert("我是body的单击响应函数");
  135. };*/
  136. </script>
  137. <pre>
  138. 114. 事件的委托
  139. - 指将事件统一绑定给元素的共同的祖先元素,这样当后代元素上的事件触发时,会一直冒泡到祖先元素从而通过祖先元素的响应函数来处理事件。
  140. - 事件委派是利用了冒泡,通过委派可以减少事件绑定的次数,提高程序的性能
  141. </pre>
  142. <button id="btn01">添加超链接</button>
  143. <ul id="u1" style="background-color: #bfa;">
  144. <li><p>我是p元素</p></li>
  145. <li><a href="javascript:;" class="link">超链接一</a></li>
  146. <li><a href="javascript:;" class="link">超链接二</a></li>
  147. <li><a href="javascript:;" class="link">超链接三</a></li>
  148. </ul>
  149. <script type="text/javascript">
  150. console.log("114");
  151. var btn01 = document.getElementById("btn01")
  152. var u1 = document.getElementById("u1")
  153. var n=1
  154. btn01.onclick = function(event){
  155. event = event || window.event
  156. var newli = document.createElement("li")
  157. newli.innerHTML = "<a href='javascript:;' class='link'>新建的超链接" + n + "</a>"
  158. u1.appendChild(newli)
  159. n++
  160. event.cancelBubble = true
  161. }
  162. /*
  163. * 为每一个超链接都绑定一个单击响应函数
  164. * 这里我们为每一个超链接都绑定了一个单击响应函数,这种操作比较麻烦,
  165. * 而且这些操作只能为已有的超链接设置事件,而新添加的超链接必须重新绑定
  166. */
  167. var allA = document.getElementsByTagName("a")
  168. /*for( var i=0,l=allA.length; i<l; i++){
  169. allA[i].onclick = function(){
  170. alert(this.innerHTML)
  171. }
  172. }*/
  173. // 我们希望,只绑定一次事件,即可应用到多个的元素上,即使元素是后添加的
  174. // 我们可以尝试将其绑定给元素的共同的祖先元素
  175. u1.onclick = function(event){
  176. event = event || window.event
  177. // event中的target表示的触发事件的对象
  178. //alert(event.target)
  179. //如果触发事件的对象是我们期望的元素,则执行否则不执行
  180. if(event.target.className == 'link'){
  181. alert(event.target.innerHTML)
  182. }
  183. }
  184. </script>
  185. <pre>
  186. 115. 事件的绑定
  187. 使用 对象.事件 = 函数 的形式绑定响应函数,
  188. 它只能同时为一个元素的一个事件绑定一个响应函数,
  189. 不能绑定多个,如果绑定了多个,则后边会覆盖掉前边的。
  190. addEventListener()
  191. - 通过这个方法也可以为元素绑定响应函数
  192. - 参数:
  193. 1.事件的字符串,不要on
  194. 2.回调函数,当事件触发时该函数会被调用
  195. 3.是否在捕获阶段触发事件,需要一个布尔值,一般都传false
  196. 使用addEventListener()可以同时为一个元素的相同事件同时绑定多个响应函数,
  197. 这样当事件被触发时,响应函数将会按照函数的绑定顺序执行
  198. 这个方法不支持IE8及以下的浏览器。
  199. attachEvent()
  200. - IE8中可以使用attachEvent()来绑定事件
  201. - 参数:
  202. 1.事件的字符串,要on
  203. 2.回调函数
  204. - 这个方法也可以同时为一个事件绑定多个处理函数,
  205. 不同的是它是后绑定先执行,执行顺序和addEventListener()相反
  206. </pre>
  207. <div class="">
  208. <button id="bdBtn">绑定按钮</button>
  209. </div>
  210. <script type="text/javascript">
  211. console.log("115");
  212. var bdBtn = document.getElementById("bdBtn")
  213. /*bdBtn.onclick = function(){
  214. alert(1)
  215. }
  216. bdBtn.onclick = function(){
  217. alert(2)
  218. }*/
  219. bdBtn.addEventListener("click",function(){
  220. alert(1)
  221. },false)
  222. bdBtn.addEventListener("click",function(){
  223. alert(2)
  224. },false)
  225. /*bdBtn.attachEvent("onclick",function(){
  226. alert(this)
  227. })
  228. bdBtn.attachEvent("onclick",function(){
  229. alert(this)
  230. })*/
  231. </script>
  232. <pre>
  233. 116. 完成bind函数
  234. </pre>
  235. <div class="">
  236. <button id="bdBtn2">bind按钮</button>
  237. </div>
  238. <script type="text/javascript">
  239. console.log("116");
  240. //定义一个函数,用来为指定元素绑定响应函数
  241. /*
  242. * addEventListener()中的this,是绑定事件的对象
  243. * attachEvent()中的this,是window
  244. * 需要统一两个方法this
  245. */
  246. /*
  247. * 参数:
  248. * obj 要绑定事件的对象
  249. * eventStr 事件的字符串(不要on)
  250. * callback 回调函数
  251. */
  252. function bind(obj,eventStr,callback){
  253. if( obj.addEventListener ){
  254. // 大部分浏览器兼容的方式
  255. obj.addEventListener( eventStr,callback,false)
  256. }else{
  257. /*
  258. * this是谁由调用方式决定
  259. * callback.call(obj)
  260. */
  261. obj.attachEvent( "on"+eventStr , function(){
  262. //在匿名函数中调用回调函数
  263. callback.call(obj)
  264. })
  265. }
  266. }
  267. var bdBtn2 = document.getElementById("bdBtn2")
  268. bind(bdBtn2,"click",function(){
  269. alert(1)
  270. })
  271. bind(bdBtn2,"click",function(){
  272. alert(2)
  273. })
  274. bind(bdBtn2,"click",function(){
  275. alert(this)
  276. })
  277. </script>
  278. <pre>
  279. 117. 事件的传播
  280. - 关于事件的传播网景公司和微软公司有不同的理解
  281. - 微软公司认为事件应该是由内向外传播,也就是当事件触发时,应该先触发当前元素上的事件,
  282. 然后再向当前元素的祖先元素上传播,也就说事件应该在冒泡阶段执行。
  283. - 网景公司认为事件应该是由外向内传播的,也就是当前事件触发时,应该先触发当前元素的最外层的祖先元素的事件,
  284. 然后在向内传播给后代元素
  285. - W3C综合了两个公司的方案,将事件传播分成了三个阶段
  286. 1.捕获阶段
  287. - 在捕获阶段时从最外层的祖先元素,向目标元素进行事件的捕获,但是默认此时不会触发事件
  288. 2.目标阶段
  289. - 事件捕获到目标元素,捕获结束开始在目标元素上触发事件
  290. 3.冒泡阶段
  291. - 事件从目标元素向他的祖先元素传递,依次触发祖先元素上的事件
  292. - 如果希望在捕获阶段就触发事件,可以将addEventListener()的第三个参数设置为true
  293. 一般情况下我们不会希望在捕获阶段触发事件,所以这个参数一般都是false
  294. - IE8及以下的浏览器中没有捕获阶段
  295. </pre>
  296. <style type="text/css">
  297. #cbbox1{
  298. width: 300px;
  299. height: 300px;
  300. background-color: yellowgreen;
  301. }
  302. #cbbox2{
  303. width: 200px;
  304. height: 200px;
  305. background-color: yellow;
  306. }
  307. #cbbox3{
  308. width: 150px;
  309. height: 150px;
  310. background-color: skyblue;
  311. }
  312. </style>
  313. <div id="cbbox1">cbbox1
  314. <div id="cbbox2">cbbox2
  315. <div id="cbbox3">cbbox3</div>
  316. </div>
  317. </div>
  318. <script type="text/javascript">
  319. console.log("117");
  320. var cbbox1 = document.getElementById("cbbox1")
  321. var cbbox2 = document.getElementById("cbbox2")
  322. var cbbox3 = document.getElementById("cbbox3")
  323. function bind(obj,eventStr,callback){
  324. if( obj.addEventListener ){
  325. // 大部分浏览器兼容的方式
  326. obj.addEventListener( eventStr,callback,true) // 捕获阶段
  327. }else{
  328. /*
  329. * this是谁由调用方式决定
  330. * callback.call(obj)
  331. */
  332. obj.attachEvent( "on"+eventStr , function(){
  333. //在匿名函数中调用回调函数
  334. callback.call(obj)
  335. })
  336. }
  337. }
  338. bind(cbbox1,"click",function(){
  339. alert("我是cbbox1的响应函数")
  340. })
  341. bind(cbbox2,"click",function(){
  342. alert("我是cbbox2的响应函数")
  343. })
  344. bind(cbbox3,"click",function(){
  345. alert("我是cbbox3的响应函数")
  346. })
  347. </script>
  348. <pre>
  349. 118. 拖拽1
  350. - 拖拽的流程
  351. 1.当鼠标在被拖拽元素上按下时,开始拖拽 onmousedown
  352. 2.当鼠标移动时被拖拽元素跟随鼠标移动 onmousemove
  353. 3.当鼠标松开时,被拖拽元素固定在当前位置 onmouseup
  354. </pre>
  355. <style type="text/css">
  356. #tuozhuai_box {
  357. position: relative;
  358. width: 1000px;
  359. height: 500px;
  360. }
  361. #tzbox1{
  362. width: 100px;
  363. height: 100px;
  364. background-color: red;
  365. position: absolute;
  366. }
  367. #tzbox2{
  368. width: 100px;
  369. height: 100px;
  370. background-color: yellow;
  371. position: absolute;
  372. left: 200px;
  373. top: 200px;
  374. }
  375. </style>
  376. <div id="tuozhuai_box">
  377. <div id="tzbox1"></div>
  378. <div id="tzbox2"></div>
  379. </div>
  380. <script type="text/javascript">
  381. console.log("118");
  382. var tzBox = document.getElementById("tuozhuai_box")
  383. var tzbox1 = document.getElementById("tzbox1")
  384. tzbox1.onmousedown = function(event){
  385. event = event || window.event
  386. var ol = event.clientX - tzbox1.offsetLeft
  387. var ot = event.clientY - tzbox1.offsetTop
  388. tzBox.onmousemove = function(event){
  389. event = event || window.event
  390. var left = event.clientX - ol
  391. var top = event.clientY - ot
  392. tzbox1.style.left = left + "px"
  393. tzbox1.style.top = top + "px"
  394. }
  395. tzBox.onmouseup = function(){
  396. tzBox.onmousemove = null
  397. tzBox.onmouseup = null
  398. }
  399. }
  400. </script>
  401. <pre>
  402. 119. 拖拽2
  403. </pre>
  404. <style type="text/css">
  405. #tuozhuai_box2 {
  406. position: relative;
  407. width: 1000px;
  408. height: 500px;
  409. }
  410. #tzbox21{
  411. width: 100px;
  412. height: 100px;
  413. background-color: red;
  414. position: absolute;
  415. }
  416. #tzbox22{
  417. width: 100px;
  418. height: 100px;
  419. background-color: yellow;
  420. position: absolute;
  421. left: 200px;
  422. top: 200px;
  423. }
  424. </style>
  425. <div id="tuozhuai_box2">
  426. <div id="tzbox21"></div>
  427. <div id="tzbox22"></div>
  428. </div>
  429. <script type="text/javascript">
  430. console.log("119");
  431. var tzBox2 = document.getElementById("tuozhuai_box2")
  432. var tzbox21 = document.getElementById("tzbox21")
  433. tzbox21.onmousedown = function(event){
  434. //设置tzbox21捕获所有鼠标按下的事件
  435. /*
  436. * setCapture()
  437. * - 只有IE支持,但是在火狐中调用时不会报错,
  438. * 而如果使用chrome调用,会报错
  439. */
  440. /*if(tzbox21.setCapture){
  441. tzbox21.setCapture();
  442. }*/
  443. tzbox21.setCapture && tzbox21.setCapture();
  444. event = event || window.event
  445. var ol = event.clientX - tzbox21.offsetLeft
  446. var ot = event.clientY - tzbox21.offsetTop
  447. tzBox2.onmousemove = function(event){
  448. event = event || window.event
  449. var left = event.clientX - ol
  450. var top = event.clientY - ot
  451. tzbox21.style.left = left + "px"
  452. tzbox21.style.top = top + "px"
  453. }
  454. tzBox2.onmouseup = function(){
  455. tzBox2.onmousemove = null
  456. tzBox2.onmouseup = null
  457. tzbox21.releaseCapture && tzbox21.releaseCapture();
  458. }
  459. /*
  460. * 当我们拖拽一个网页中的内容时,浏览器会默认去搜索引擎中搜索内容,
  461. * 此时会导致拖拽功能的异常,这个是浏览器提供的默认行为,
  462. * 如果不希望发生这个行为,则可以通过return false来取消默认行为
  463. * 但是这招对IE8不起作用
  464. */
  465. return false
  466. }
  467. </script>
  468. <pre>
  469. 120. 拖拽3
  470. 完成drag函数
  471. </pre>
  472. <script type="text/javascript">
  473. console.log("120");
  474. function drag(obj){
  475. obj.onmousedown = function(event){
  476. obj.setCapture && obj.setCapture();
  477. event = event || window.event
  478. var ol = event.clientX - obj.offsetLeft
  479. var ot = event.clientY - obj.offsetTop
  480. document.onmousemove = function(event){
  481. event = event || window.event
  482. var left = event.clientX - ol
  483. var top = event.clientY - ot
  484. obj.style.left = left + "px"
  485. obj.style.top = top + "px"
  486. }
  487. document.onmouseup = function(){
  488. document.onmousemove = null
  489. document.onmouseup = null
  490. obj.releaseCapture && obj.releaseCapture();
  491. }
  492. return false
  493. }
  494. }
  495. var tzbox22 = document.getElementById("tzbox22")
  496. drag(tzbox22)
  497. </script>
  498. </body>
  499. </html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值