用JavaScript实现3D轮转

js代码具体实现:

// JavaScript Document
window.onload = function(){  
    //定义一个旋转类
    var rt = new imgRound("imgContainer", 120, 90, 300, 80, 230, 0.01);
    //然后使用定时函数调用imgRound实例的roundMove方法
    setInterval(function(){
                rt.roundMove()
            }, 20)
    }   
    //参数说明:容器id,宽度,高度,圆心水平位置,圆心垂直位置,圆圈半径,旋转速度,垂直距离(默认为10.可选),旋转方向(1:顺时针2:逆时针 可选 默认为1)
    function imgRound(id, w, h, x, y, r, dv, rh, ah){
            if(ah == undefined) ah = 1;
            if(rh == undefined) rh = 10;
            var dv = dv * ah; //旋转速度
            var pi = 3.1415926575;            
            var d = pi / 2;            
            var pd = Math.asin(w / 2 / r);//返回参数的正弦值            
            var smove = true;            
            var imgArr = new Array();            
            var objectId = id;            
            var o = document.getElementById(objectId);
                o.style.position = "relative";            
            var arrimg = o.getElementsByTagName("img");           
            var pn = arrimg.length; //图片数量
            var ed = pi * 2 / pn;    

            for(n = 0; n < arrimg.length; n++) { 

                var lk = arrimg[n].getAttribute("link");                
                if(lk != null) 
                    arrimg[n].setAttribute("title", lk)
                    arrimg[n].onclick = function(){                   
                        if(this.getAttribute("link") != null) {                        
                            if(this.getAttribute("target") != "_blank") 
                                window.location = (this.getAttribute("link"))                   
                            else 
                                window.open(this.getAttribute("link"))
                            }
                    }
                    arrimg[n].onmouseout = function() {
                        smove = true;
                    }
                    arrimg[n].onmouseover = function() {
                        smove = false;
                     }
                arrimg[n].style.position = "absolute";
                imgArr.push(arrimg[n]);
            }           
            this.roundMove = function() {                
            for(n = 0; n <= pn - 1; n++) {                   
            var o = imgArr[n];                   
            var ta = Math.sin(d + ed * n),strFilter;                   
                        if(ta < 0) 
                            o.style.left = Math.cos(d + ed * n - pd) * r + x + "px";                        else 
                            o.style.left = Math.cos(d + ed * n + pd) * r + x + "px";
                            o.style.top = ta * rh + rh + y + "px";                   
                        var zoom = Math.abs(Math.sin((d + ed * n) / 2 + pi / 4)) * 0.5 + 0.5;
                            o.style.width = Math.abs(Math.cos(d + ed * n + pd) - Math.cos(d + ed * n - pd)) * zoom * r + "px";
                            o.style.height = zoom * h + "px";                    
                    if(ta < 0) {
                        ta = (ta + 1) * 80 + 20;
                        o.style.zIndex = 0;
                    } else {
                        ta = 100;
                        o.style.zIndex = 1
                    }                    
                    if(o.style.zIndex <= 0) 
                        strFilter = "FlipH(enabled:true)"
                    else 
                        strFilter = "FlipH(enabled:false)";
                    strFilter = strFilter + " alpha(opacity=" + ta + ")";
                    o.style.opacity = ta / 100;
                    o.style.filter = strFilter;
                }               
                if(smove) d = d + dv;
            }
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaScript和CSS来实现网页图片轮转切换: 1. 首先,你需要在HTML中创建一个包含图片的容器,例如div元素。 2. 在CSS中,你需要设置容器的宽度和高度,并将其中的图片设置为绝对定位,以便它们可以在容器中移动。 3. 接下来,你可以创建一个JavaScript函数来实现轮转切换。该函数应该接受一个参数,即要显示的下一张图片的索引。在函数内部,你可以使用CSS的transform属性来移动当前显示的图片,并将下一张图片移动到可见区域。 4. 最后,你可以使用setInterval函数来定期调用该函数,以便自动轮转图片。 下面是一个简单的示例代码,可以帮助你入门: HTML: ```html <div id="image-container"> <img src="image1.jpg" class="active"> <img src="image2.jpg"> <img src="image3.jpg"> </div> ``` CSS: ```css #image-container { position: relative; width: 500px; height: 300px; } #image-container img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 1s ease-in-out; } #image-container img.active { opacity: 1; } ``` JavaScript: ```javascript var currentIndex = 0; var images = document.querySelectorAll('#image-container img'); var intervalId = setInterval(changeImage, 3000); function changeImage() { currentIndex = (currentIndex + 1) % images.length; images[currentIndex].classList.add('active'); images[currentIndex - 1].classList.remove('active'); } ``` 在这个示例中,我们首先定义了一个变量currentIndex来跟踪当前显示的图片的索引。然后,我们使用document.querySelectorAll函数选择所有的图片元素,并将它们存储在一个名为images的数组中。接下来,我们使用setInterval函数每3秒钟调用一次changeImage函数。该函数使用currentIndex变量来确定要显示的下一张图片的索引,并使用classList.add和classList.remove方法来添加和删除“active”类,以便将当前显示的图片和下一张图片的透明度进行切换。 请注意,这只是一个简单的示例,可以根据你的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值