算法题
https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
var getIntersectionNode = function(headA, headB) {if (!headA || !headB) return null;var head1 = headA;var head2 = headB;while(head1 !== head2) {head1 = !head1 ? headB : head1.next;head2 = !head2 ? headA : head2.next;}return head1};
解法:
双指针
手写题
写一个拖拽组件
<!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><style>body {position: relative;margin: 0;padding: 0}#box {position: absolute;width: 120px;height: 120px;background-color: red;}</style></head><body><div id="box"></div><script>window.onload = function () {// drag处于绝对定位状态let drag = document.getElementById("box");drag.onmousedown = function (e) {var e = e || window.event;// 鼠标与拖拽元素边界的距离 = 鼠标与可视区边界的距离 - 拖拽元素与边界的距离let diffX = e.clientX - drag.offsetLeft;let diffY = e.clientY - drag.offsetTop;drag.onmousemove = function (e) {// 拖拽元素移动的距离 = 鼠标与可视区边界的距离 - 鼠标与拖拽元素边界的距离let left = e.clientX - diffX;let top = e.clientY - diffY;// 避免拖拽出可视区if (left < 0) {left = 0;} else if (left > window.innerWidth - drag.offsetWidth) {left = window.innerWidth - drag.offsetWidth;}if (top < 0) {top = 0;} else if (top > window.innerHeight - drag.offsetHeight) {top = window.innerHeight - drag.offsetHeight;}drag.style.left = left + "px";drag.style.top = top + "px";};drag.onmouseup = function (e) {this.onmousemove = null;this.onmouseup = null;};};};</script></body></html>
注意边界处理
