测试浏览器为谷歌浏览器(谷歌toggle device toolbar)
var startx, starty, endx, endy, moveX, moveY, seatX, seatY; var clickState = false; //获取输入框dom元素 var text = document.forms[“form”]; //设置样式 function setCss(obj) { var cssStr = “z-index:5;width:37px;height:37px;position:absolute;left:” + seatX + ‘px;top:' + seatY + ‘px;'; //将样式添加到div上,显示div obj.style.cssText = cssStr;}//计算位置 function setPosition(obj) { if (obj == ‘left') { seatX = text.offsetLeft - 37; //横坐标 } else { seatX = text.offsetLeft + text.offsetWidth; //横坐标 } seatY = (text.offsetTop + text.offsetHeight) / 2; //纵坐标 } //创建DIV function createDiv(obj) { //首先创建div var descDiv = document.createElement(‘div'); document.body.appendChild(descDiv); //给div设置样式,比如大小、位置 setPosition(obj); setCss(descDiv); descDiv.innerHTML = ”; descDiv.id = obj; descDiv.style.display = ‘block'; addElementImg(descDiv.id); } //添加IMG function addElementImg(obj) { var div = document.getElementById(obj); //添加 img var img = document.createElement(“img”); //设置 img 属性,如 id img.setAttribute(“id”, “newImg”); //设置 img 图片地址 img.src = “/Themes/TheThemeMachine/Images/” + obj + “.webp”; div.appendChild(img); } //删除DIV function removeDiv(obj) { var el = document.getElementById(obj); el.parentNode.removeChild(el); } //移动DIV function moveDiv(obj, movex) { if (Math.abs(movex) < 37) { var div = document.getElementById(obj); setPosition(obj); seatX = seatX + movex; setCss(div); } } //根据位移改变DIV的位置 function reductionDiv(obj) { var div = document.getElementById(obj); setPosition(obj); setCss(div); } //计算移动坐标 function calculationMoveCoordinate() { moveX = endx - startx; moveY = Math.abs(endy - starty); if (moveX > 0) moveDiv(“left”, moveX); else moveDiv(“right”, moveX); } //判断是否是PC端 function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array(“Android”, “iPhone”, “SymbianOS”, “Windows Phone”, “iPad”, “iPod”); var flag = true; for (var v = 0; v < Agents.length; v++) { if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; } } return flag; } //PC端鼠标按下 function click() { clickState = true; startx = event.clientX; starty = event.clientY; } //PC端鼠标移动 function pull() { if (1 == event.which) //判断左键是否按下 { endx = event.clientX; endy = event.clientY; calculationMoveCoordinate(); } } //PC端和移动端位移结束 function stopClick() { if (Math.abs(moveX) > 37 && moveY < 20) { if (moveX < 0) { history.(1); } else { history.(-1); } } else { reductionDiv(“left”); reductionDiv(“right”); } } ////移动端注册事件 document.addEventListener(‘touchmove', function (event) { event.preventDefault(); }, false); //touchstart事件 function touchSatrtFunc(evt) { //evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等 var touch = evt.touches[0]; //获取第一个触点 startx = Number(touch.pageX); //页面触点X坐标 starty = Number(touch.pageY); //页面触点Y坐标 }//touchmove事件,这个事件无法获取坐标 function touchMoveFunc(evt) { //evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等 var touch = evt.touches[0]; //获取第一个触点 endx = Number(touch.pageX); //页面触点X坐标 endy = Number(touch.pageY); //页面触点Y坐标 calculationMoveCoordinate(); }//touchend事件 function touchEndFunc(evt) { //evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等 stopClick(); }//加载 if (IsPC()) { document.onmousedown = click; document.onmousemove = pull; document.onmouseup = stopClick; } else { document.addEventListener(‘touchstart', touchSatrtFunc, false); document.addEventListener(‘touchmove', touchMoveFunc, false); document.addEventListener(‘touchend', touchEndFunc, false); } createDiv(‘left'); createDiv(‘right');
以上所述是小编给大家介绍的JS实现仿UC浏览器前进后退效果的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!