• 不浮夸且不单调——监听鼠标图片移动动画


    鼠标监听实现图片动画

      这是一个小的动画,监听鼠标的移动,来实现 图片的移动视觉特效。虽然功能不是那么的强大,但应用范围还是很广泛的,不浮夸且不是单调。

    先给大家欣赏一下样式。

     
    小样子
     
     


    代码:
     
     
    <!doctype html>
    <html>
    
        <head>
            <meta charset="utf-8">
            <title>鼠标移动图片</title>
            <style>
                body {
                    margin: 0;
                    background-image: url(img/beijing.jpg);
                }
                
                #wrap {
                     100%;
                    height: 800px;
                    margin: 30px auto;
                    
                    position: relative;
                }
                
                #wrap img {
                    position: absolute;
                }
                
                #wrap img:nth-of-type(1){
                     200px;
                    height: 300px;
                    left: 300px;
                    top: 30px;
                    z-index: 0;
        
                }
                #wrap img:nth-of-type(2){
                     150px;
                    height: 200px;
                    left: 200px;
                    top: 300px;
                    z-index: 1;
        
                }
                #wrap img:nth-of-type(3){
                     150px;
                    height: 190px;
                    right: 100px;
                    top: 50px;
                    z-index:2;
        
                }
                #wrap img:nth-of-type(4){
                     200px;
                    height: 200px;
                    left: 400px;
                    top: 30px;
                    z-index: 3;
        
                }
                #wrap img:nth-of-type(5){
                     200px;
                    height: 200px;
                    right: 500px;
                    top: 70px;
                    z-index: 4;
        
                }
                #wrap img:nth-of-type(6){
                     200px;
                    height: 200px;
                    right: 200px;
                    top: 200px;
                    z-index: 5;
        
                }
                #wrap img:nth-of-type(7){
                     400px;
                    height: 400px;
                    right: 35%;
                    top: 200px;
                    z-index: 6;
        
                }
            </style>
        </head>
    
        <body>
            <div id="wrap">
                <img  src="img/图像(2).png"/>
                <img  src="img/图像(3).png"/>
                <img  src="img/dada_man_ray_skulptur_cadeau_01.gif"/>
                <img  src="img/图像(5).png"/>
                <img  src="img/图像(6).png"/>
                <img  src="img/capture_decran_2016_03_16_a_151542.jpg"/>
                <img  src="img/图像(8).png"/>
            </div>
            <script>
                var oWrap = document.getElementById("wrap");
                var aImg = oWrap.getElementsByTagName("img");
                var iMax = 7;
                //获取图片的初始位置
                for(var i = 0; i < aImg.length; i++) {
                    aImg[i].startX = parseInt(getStyle(aImg[i], 'left'))
                }
                oWrap.onmousemove = function(ev) {
                    ev = ev || window.event;
                    //获取鼠标的位置与div中心点位置的距离
                    var Yd = ev.clientX - (oWrap.offsetLeft + this.offsetWidth / 5)
                    for(var i = 0; i < aImg.length; i++) {
                        //获取每个img的z-index
                        var iZindex = getStyle(aImg[i], 'zIndex')
                        //Zindex越大移动的相对距离越小
                        var iDisL = -parseInt(Yd / iMax * (iMax - iZindex) / 20)
                        //图片的距离等于原先的距离加上计算的距离
                        aImg[i].style.left = aImg[i].startX + iDisL + 'px'
                    }
                }
    
                function getStyle(obj, attr) {
                    if(obj.currentStyle) {
              //IE浏览器
    return obj.currentStyle[attr]; } return getComputedStyle(obj)[attr]; } </script> </body> </html>

    复制代码后记得更改一下图片,我设置的是监听#wrap范围的鼠标,只是监听x轴的,大家可以研究添加y轴的监听移动。

     

     
    最后

        如果有更好的方式方法,期待大家分享,共同学习,共同进步。

  • 相关阅读:
    STL标准库algorithm中remove()函数的一个小注意事项
    关于 mem_fun_ref 和 bind2nd的疑问
    记录昨日程序调不通的解释
    复习几个C++概念:声明与定义、传值与拷贝构造、初始化和赋值
    对stl map 赋值添加元素的疑惑 求解(管理员让这个帖子多见会人吧~~谢谢啦!)
    摘抄书上一个逆序字符串的例子(可根据不同的符号,比如*&,.;来拆分单词)
    “指向指针的引用”小例子:忽然豁然开朗~
    论文 “tracking evolving communities in large linked networks” 中不懂的问题和知识总结
    [wp7软件]wp7~~时间日程 软件大全! 集合贴~~~
    [wp7游戏]wp7~~超级血腥类游戏~~集合贴~~
  • 原文地址:https://www.cnblogs.com/lnzixin/p/7787198.html
Copyright © 2020-2023  润新知