自己做的网站手机不能看,做网站需要准备什么,win10本地安装wordpress,胶南网手撸源代码如下#xff1a;注释应该很详细了#xff0c;拿去直用
可以放到在线编辑器测试#xff0c;记得修改图片路径
菜鸟教程在线编辑器
html langenheadmeta charsetUTF-8meta nameviewport conten… 手撸源代码如下注释应该很详细了拿去直用
可以放到在线编辑器测试记得修改图片路径
菜鸟教程在线编辑器
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/titlestyle#img_box {position: relative;width: 800px;height: 800px;border: 1px solid #000000;overflow: hidden;cursor: grab;}#img_c {width: 100%;height: 100%;position: absolute;object-fit: contain;user-select: none;-webkit-user-drag: none;user-select: none;-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;/* transition: all 0.1s ease-in-out; */}/style
/headbodydiv idimg_boximg idimg_c title没有 src./1719194464803.jpg/divscriptinitImageScaleElement();function initImageScaleElement() {//定义参数const max 20;//放缩原图倍数const scale 0.05;//滚轮一下缩放的变化比例 越大放缩越快//获取对象const box document.getElementById(img_box);const img document.getElementById(img_c);/*** 图片已加载完毕后,对图片居中* 图片一开始其实默认是居中的只不过是img的object-fit: contain属性进行了居中并且产生了一些边缘填充空白* 这些填充的空白会影响后续图片拖拽的计算因此需要将img修改成图片展示的真实大小但对用户视觉不发生改变。*/img.onload function () {const imgRatio img.naturalWidth / img.naturalHeight; //图片横纵比const boxRatio box.clientWidth / box.clientHeight; //容器横纵比/*** 因为拿到的图片真实大小不是显示大小需要手动放缩适配到容器中* 当图片横纵比大于容器横纵比图片保持比例的情况下填充容器此时只能对图片的横向放缩填充全部容器 * 当图片横纵比小于容器横纵比图片保持比例的情况下填充容器此时只能对图片的纵向放缩填充全部容器 * 这个规律可以自己画图试验一下*/if (imgRatio boxRatio) { const scale box.clientWidth / img.naturalWidth;img.style.width box.clientWidth; //长度填充img.style.height img.naturalHeight * scale; //高度自适应img.style.top Math.ceil((box.clientHeight - img.clientHeight) / 2) px ;//位置居中} else {const scale box.clientHeight / img.naturalHeight;img.style.height box.clientHeight;//高度填充img.style.width img.naturalWidth * scale;//长度自适应img.style.left Math.ceil((box.clientWidth - img.clientWidth) / 2) px;//位置居中}};//用于元素拖拽时边界的参数限制入参{ 实际值左区间右区间 }const getRange (actual, limita, limitb) {if (actual -limita) {return -limita;} else if (actual limitb) {return limitb;}return actual;}/*** 缩放操作* */box.onwheel (event) {event.preventDefault(); //关闭默认事件const center_x event.clientX;const center_y event.clientY;const wheelDelta event.deltaY;const top_d center_y - img.offsetTop;const left_d center_x - img.offsetLeft;if (wheelDelta 0) {//缩小 往后推滚轮 【滚轮值和缩放关系不是固定的你也可以反过来】let modifyHeight img.clientHeight * (1 - scale);let modifyWidth img.clientWidth * (1 - scale);if (modifyHeight * max img.naturalHeight) { //只在比例范围内放缩img.style.height modifyHeight;img.style.width modifyWidth;img.style.top center_y - top_d * (1 - scale);img.style.left center_x - left_d * (1 - scale);} else {console.log(缩小超出 max * 100 %比例无法缩小);}} else {//放大 往前推滚轮let modifyHeight img.clientHeight * (1 scale);let modifyWidth img.clientWidth * (1 scale);if (modifyHeight img.naturalHeight * max) { //只在比例范围内放缩img.style.height modifyHeight;img.style.width modifyWidth;img.style.top center_y - top_d * (1 scale);img.style.left center_x - left_d * (1 scale);} else {console.log(放大超出 max * 100 %比例无法放大);}}}/*** 拖拽操作*///拖拽需要的全局变量const drag {status: false,lastX: null,lastY: null}box.onmousedown (event) {if (event.button 0) {//鼠标会移出元素甚至移出到浏览器外部使用document监听移动就可以不受影响。document.onmousemove (event) {if (drag.status) {let mx event.clientX - drag.lastXlet my event.clientY - drag.lastYdrag.lastX event.clientXdrag.lastY event.clientYlet top img.offsetTop mylet left img.offsetLeft mx//拖拽超出容器外部无意义可能找不回因此图片位置不能任意拖拽至少有一个角或一个边在容器内部这里减10就是预留出的宽度img.style.left getRange(left, img.clientWidth - 10, box.clientWidth - 10) px;img.style.top getRange(top, img.clientHeight - 10, box.clientHeight - 10) px;//如果你不想让图片跑出容器那就调整这里就好了控制两个相邻边就能控制整个图片如// img.style.left getRange(left,0, box.clientWidth - img.clientWidth) px;// img.style.top getRange(top,0, box.clientHeight - img.clientHeight) px;}}//鼠标松开初始化操作如果鼠标在元素外部松开元素的松开监听是获取不到的因此需要用document监听document.onmouseup (event) {if (event.button 0) {drag.status falsedocument.onmousemove nulldocument.onmouseup null}}drag.status truedrag.lastX event.clientXdrag.lastY event.clientY}}}/script/body/html 文章转载自: http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.frfpx.cn.gov.cn.frfpx.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.pbzgj.cn.gov.cn.pbzgj.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.lfpzs.cn.gov.cn.lfpzs.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.mbfkt.cn.gov.cn.mbfkt.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.rpkg.cn.gov.cn.rpkg.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.rgksz.cn.gov.cn.rgksz.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.rfqk.cn.gov.cn.rfqk.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn