html中图片展览,HTML5 3D图片空间/展览

该博客详细介绍了如何使用JavaScript实现屏幕元素的拖放功能和响应式布局调整。通过ge1doot库,作者展示了如何初始化屏幕尺寸,监听窗口大小变化,并实现平滑的动画效果。同时,还涉及到了触摸事件的处理,以及 Ease 类用于平滑过渡的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

var ge1doot = ge1doot || {

screen: {

elem: null,

callback: null,

width: 0,

height: 0,

left: 0,

top: 0,

init: function(id, callback, initRes) {

this.elem = document.getElementById(id);

this.callback = callback || null;

window.addEventListener('resize', function() {

ge1doot.screen.resize();

}, false);

initRes && this.resize();

},

resize: function() {

var o = this.elem;

this.width = o.offsetWidth;

this.height = o.offsetHeight;

for (this.left = 0, this.top = 0; o != null; o = o.offsetParent) {

this.left += o.offsetLeft;

this.top += o.offsetTop;

}

this.callback && this.callback();

}

},

drag: {

screen: null,

x: 0,

y: 0,

xs: 0,

ys: 0,

bx: 0,

by: 0,

xp: 0,

yp: 0,

active: false,

down: function(e, touch) {

e.preventDefault();

var pointer = touch ? e.touches[0] : e;

(!touch && document.setCapture) && document.setCapture();

this.xp = this.xs = pointer.clientX;

this.yp = this.ys = pointer.clientY;

this.active = true;

},

up: function(e, touch) {

e.preventDefault();

(!touch && document.releaseCapture) && document.releaseCapture();

this.bx = this.x;

this.by = this.y;

this.active = false;

},

move: function(e, touch) {

e.preventDefault();

if (this.active) {

var pointer = touch ? e.touches[0] : e;

this.xp = pointer.clientX;

this.yp = pointer.clientY;

this.x = this.bx - (this.xp - this.xs);

this.y = this.by - (this.yp - this.ys);

}

},

init: function(screen) {

var self = this;

this.screen = screen.elem;

if ('ontouchstart' in window) {

// touch

this.screen.ontouchstart = function(e) {

self.down(e, true);

}

this.screen.ontouchmove = function(e) {

self.move(e, true);

}

this.screen.ontouchend = function(e) {

self.up(e, true);

}

this.screen.ontouchcancel = function(e) {

self.up(e, true);

}

}

// mouse

document.addEventListener("mousedown", function(e) {

self.down(e, false);

}, true);

document.addEventListener("mousemove", function(e) {

self.move(e, false);

}, true);

document.addEventListener("mouseup", function(e) {

self.up(e, false);

}, true);

}

},

Ease: function(speed, val) {

this.speed = speed;

this.target = val;

this.value = val;

}

}

ge1doot.Ease.prototype.ease = function(target) {

this.value += (target - this.value) * this.speed;

}

! function() {

window.addEventListener('load', function() {

"use strict";

var Xi = 0,

Yi = 0,

Zi = 0,

rotation;

var faces, localTransform = [];

var screen = ge1doot.screen;

var drag = ge1doot.drag;

var perp = new ge1doot.Ease(0.01, 50);

// ==== init script ====

screen.init("screen", function() {}, true);

drag.init(screen);

faces = document.getElementById("scene").getElementsByTagName("img");

rotation = {

ex: 0,

ey: 0,

x: 0,

y: 0,

tz: 0,

tx: 0,

ttx: 0,

tty: 0,

ttz: 0,

speedx: 0.1,

speedz: 0.1,

ease: function(x, y) {

this.y = -(this.ey += (x - this.ey) * 0.06) / 3;

this.x = (this.ex += (y - this.ex) * 0.06) / 3;

var a = this.y * Math.PI / 180;

var x = -Math.sin(a) * this.speedx;

var z = Math.cos(a) * this.speedz;

this.tx += x;

this.tz += z;

if (drag.active) {

if ((this.tx > 260 && x > 0) || (this.tx < -260 && x < 0)) this.speedx *= 0.9;

else {

if (this.speedx < 0.1) this.speedx = 1;

if (this.speedx < 5) this.speedx *= 1.1;

}

if ((this.tz > 260 && z > 0) || (this.tz < -260 && z < 0)) this.speedz *= 0.9;

else {

if (this.speedz < 0.1) this.speedz = 1;

if (this.speedz < 5) this.speedz *= 1.1;

}

} else {

this.speedx *= 0.9;

this.speedz *= 0.9;

}

a = Math.cos(this.x * Math.PI / 180);

this.ttx = -(Math.cos((this.y - 90) * Math.PI / 180) * a) * 400;

this.ttz = -(Math.sin((this.y - 90) * Math.PI / 180) * a) * 400;

this.tty = Math.sin(this.x * Math.PI / 180) * 100;

}

}

// ==== init faces ====

for (var i = 0, n = faces.length; i < n; i++) {

var elem = faces[i];

var s = elem.getAttribute("data-transform");

elem.style.transform = s;

elem.style.webkitTransform = s;

elem.style.visibility = "visible";

localTransform.push(s);

}

// ==== main loop ====

function run() {

requestAnimationFrame(run);

perp.ease(drag.active ? 300 : 500);

if (drag.y > 270) drag.y = drag.by = 270;

if (drag.y < -270) drag.y = drag.by = -270;

rotation.ease(drag.x, drag.y);

var globalRotation = "perspective(" + perp.value + "px) rotateX(" + rotation.x + "deg) " + "rotateY(" + rotation.y + "deg) translateX(" + (rotation.tx + rotation.ttx) + "px) translateY(" + rotation.tty + "px) translateZ(" + (rotation.tz + rotation.ttz) + "px)";

// ==== anim faces ====

for (var i = 0, n = faces.length; i < n; i++) {

var elem = faces[i];

var s = globalRotation + localTransform[i];

elem.style.transform = s;

elem.style.webkitTransform = s;

}

}

// ==== start animation ====

requestAnimationFrame(run);

}, false);

}();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值