javascript狂想曲(三)

前段时间看了很多php后台代码,我真心觉得php的框架还是挺不错的,上手很快,模版语言也特别好,函数写法感觉像是java和js的混合体,有public和private,但是方法名却是function,我已经把route,model和controller的联系基本弄明白了,特别要说的是blade模版,真心比jade好用呀。。。好了,言归正传今天给大家带来一点干货吧。

1 背景图片随鼠标摆动

实现这个效果,应该怎么做,我来说一下我的思路:

  • 监听鼠标移动onmousemove事件,获取每个时刻的坐标值。
  • 通过animate更改图片的位置,每次方向发生改变动画停止,开启另一个。

思路有了,我们来看下如何实现把

<style type="text/css">
   .top{position: absolute;width: 1920px;left: 50%;margin-left: -960px;height: 582px;overflow: hidden;}
   .box{position: relative;width: 1000px;height: 582px;overflow: hidden;margin:0 auto;background:#ff6a00;}
   .pic1{width: 1000px;height: 300px;position: absolute;left: 0;top: 282px;z-index: 114;}
   .pic2{width: 1022px;height: 473px;position: absolute;right: 439px;top: 109px;z-index: 113;}
   .pic3{width: 928px;height: 582px;position: absolute;left: 380px;top: 0;z-index: 112;}
   img{vertical-align: top;}
   .pic4{width: 1000px;height: 300px;position: absolute;left: 0;top: 282px;z-index: 113;}
   .pic5{width: 1000px;height: 300px;position: absolute;left: 0;top: 282px;z-index: 999;}
   .main{width: 1000px;margin: 0 auto;position: relative;}
</style>

<div class="top">
   <div class="pic2" >
       ![](images/02.png)
   </div>
   <div class="pic3">
       ![](images/03a.png)
   </div>
</div>
<div class="main">
   <div class="box">
       <div class="pic1">
           ![](images/01_BG.png)
       </div>

       <div class="pic4">
           ![](images/04.png)
       </div>
       <div class="pic5">
           ![](images/05.png)
       </div>
   </div>
</div>

我们准备了5张图片,在第五张图片上滑动式会有效果。

$(function(){
    var positionX = 0;
    var positionY = 0;

    $('.pic5').mousemove(function(e) {
        var x = e.clientX, y = e.clientY;
        if(positionX === 0 && positionY === 0){
            positionX = x;
            positionY = y;
        }
        if(x > positionX && y < positionY){
            $('.pic1').stop().animate({'left':10,'top':292},'800',"easeOutCubic");
            $('.pic2').stop().animate({'right':449,'top':114},'800',"easeOutCubic");
            $('.pic3').stop().animate({'left':375,'top':5},'800',"easeOutCubic");
            $('.pic4').stop().animate({'left':-20,'top':300},'800',"easeOutCubic");
            positionX = x;
            positionY = y;
        }else if(x > positionX && y > positionY){
            $('.pic1').stop().animate({'left':-10,'top':280},'800',"easeOutCubic");
            $('.pic2').stop().animate({'right':429,'top':104},'800',"easeOutCubic");
            $('.pic3').stop().animate({'left':375,'top':-5},'800',"easeOutCubic");
            $('.pic4').stop().animate({'left':-20,'top':278},'800',"easeOutCubic");
            positionX = x;
            positionY = y;
        }else if(x < positionX && y < positionY){
            $('.pic1').stop().animate({'left':10,'top':292},'800',"easeOutCubic");
            $('.pic2').stop().animate({'right':449,'top':114},'800',"easeOutCubic");
            $('.pic3').stop().animate({'left':385,'top':5},'800',"easeOutCubic");
            $('.pic4').stop().animate({'left':20,'top':300},'800',"easeOutCubic");
            positionX = x;
            positionY = y;
        }else if(x < positionX && y > positionY){
            $('.pic1').stop().animate({'left':-10,'top':280},'800',"easeOutCubic");
            $('.pic2').stop().animate({'right':429,'top':104},'800',"easeOutCubic");
            $('.pic3').stop().animate({'left':385,'top':-5},'800',"easeOutCubic");
            $('.pic4').stop().animate({'left':20,'top':278},'800',"easeOutCubic");
            positionX = x;
            positionY = y;
        }

    })

    $.extend($.easing,{
        easeOutBack:function(x,t,b,c,d,s){
            if (s == undefined) s = 1.70158;
            return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
        },
        easeOutCubic: function (x, t, b, c, d) {
            return c * ((t = t / d - 1) * t * t + 1) + b;
        }
    });
})

看到这段代码,我们可以看出四种不同的方位移动都会造成4张图不一样的animate效果,在这里用到stop是停止上次执行。

2 原生js的放大镜

放大镜效果得准备2张图片,一个放小图片,一个放高清的大图片,在小图片上移动时,大图片的background-position需要作出变化。

  • 鼠标放上去的时候,大图片才显示出来。
  • 小图片上需要一个遮罩,跟随鼠标移动。
  • 每次移动时,计算坐标,控制大图的background-position。
 <style>
        .container{position: relative;margin: 20px auto;width:200px;height: 200px;border: 1px solid cadetblue;display: inline-block}
        .mirror{position: absolute;width:60px;height:60px;background: chocolate;display: none}
        .wrapper{width: 120px;height: 120px;background-image:url("qwe.jpg");background-size:auto auto;display: inline-block}
</style>

<body>
 <div class="container">
     <div class="mirror"></div>
 </div>
 
<div class="wrapper">
</div>

wrapper里面放的就是大图片,在css里面是通过background-position控制背景图片的位置。

var container = document.querySelector('.container')
     var mirror = document.querySelector('.mirror')
     var wrapper = document.querySelector('.wrapper')

     container.onmouseover = function(){
         mirror.style.display = 'block';

         document.onmousemove = function(e){
             var x = e.clientX - container.offsetLeft - mirror.offsetWidth/2;
             var y = e.clientY - container.offsetTop - mirror.offsetHeight/2;

             var maxLeft = container.offsetWidth - mirror.offsetWidth;
             var maxTop = container.offsetHeight - mirror.offsetHeight;

             if(x < 0){
                 x = 0
             }

             if(y < 0){
                 y = 0
             }

             if(x > maxLeft){
                 x = maxLeft
             }

             if(y > maxTop){
                 y = maxTop
             }

             var xRatio = -x/maxLeft*830
             var yRatio = -x/maxTop*430

             mirror.style.left = x + 'px'
             mirror.style.top = y + 'px'

             wrapper.style.backgroundPosition = ''+ xRatio + 'px ' + yRatio + 'px'
             
         }
     }
    container.onmouseout = function(){
         mirror.style.display = 'none'
     }

一个放大镜就是这么简单。

3 自定义右键菜单

我们在网页里面右键点击,会弹出原始的右键菜单,我们来实现一个自定义的菜单。

<style>
        .active{color:blue;background: orange}
</style>

<ul id="testRight" style="width: 100px;background-color: yellow;position: absolute;z-index: 100;">
    <li><a href="#">开始</a></li>
    <li><a href="#">暂停</a></li>
    <li><a href="#">拜拜</a></li>
</ul>

js代码:

window.onload=function(){
        var forRight=document.getElementById("testRight");//获取对象,现在太熟悉了
        forRight.style.display="none";
        var title=forRight.getElementsByTagName("li");

        for(var i=0;i<title.length;i++){
            title[i].onmouseover=function(){
                this.classname="active";//其实这里我们也可以调用其他事件吧
            };
            title[i].onmouseout=function(){//这里也是鼠标的两个事件吧
                this.classname="";
            };
        }

        document.oncontextmenu=function(event){//这是实现的关键点
            var event=event||window.event;//这个都不是问题了吧
            forRight.style.display="block";
            forRight.style.left=event.clientX+"px";
            forRight.style.top=event.clientY+"px";//鼠标的坐标啊
            return false;//这里返回false就是为了屏蔽默认事件
        };
        document.onclick=function(){//就是为了更形象的模仿啊
            forRight.style.display="none";
        };
    };

好了,今天就给大家带来这些,很久没写python了,之后我会写一起python爬虫的。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容