美文网首页
图片上下左右飘动(jQuery+JS)

图片上下左右飘动(jQuery+JS)

作者: 小小蒜头 | 来源:发表于2017-09-22 21:55 被阅读51次
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        img {
            position: absolute;
            width: 410px;
            height: 256px;
        }
    </style>
    <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript">
        //          //5.点击按钮,页面上图片开始飘动,但到屏幕边界时会弹向另一个方向
        //          window.onload = run;
        //          var myEvent = window.setInterval("run()", 6);
        //
        //          var x = 0;
        //          var y = 0;
        //          var xrun = true;
        //          var yrun = true;
        //          var pauseOrStart = true;
        //
        //          function run() {
        //
        //              var left_x = pageXOffset;
        //              var top_y = pageYOffset;
        //              var img = document.getElementById("img");
        //              var right_x = left_x + innerWidth - img.width;
        //              var bottom_y =  top_y + innerHeight - img.height;
        //              if(yrun) {
        //                  moveBottom();
        //              } else {
        //                  moveTop();
        //              }
        //              if(xrun) {
        //                  moveRight();
        //              } else {
        //                  moveLeft();
        //              }
        //              if(y == bottom_y) {
        //                  yrun = false;
        //              }
        //              if(y == top_y) {
        //                  yrun = true;
        //              }
        //              if(x == left_x) {
        //                  xrun = true;
        //              }
        //              if(x == right_x) {
        //                  xrun = false;
        //              }
        //              img.style.left = x + "px";
        //              img.style.top = y + "px";
        //          }
        //
        //          function moveRight() {
        //              x++;
        //
        //          }
        //
        //          function moveLeft() {
        //              x--;
        //          }
        //
        //          function moveTop() {
        //              y--;
        //          }
        //
        //          function moveBottom() {
        //              y++;
        //          }
        //
        //          function change_move() {
        //              if(pauseOrStart) {
        //                  window.clearInterval(myEvent);
        //                  pauseOrStart = false;
        //              } else {
        //                  myEvent = window.setInterval("run()", 6);
        //                  pauseOrStart = true;
        //              }
        //          }

        $(function () {
            //img的开始点
            var x = 0;
            var y = 0;
            //距离左边和上边的距离2个像素
            var x_left = 2;
            var y_top = 2;

            //获得窗体的边界
            var width = innerWidth;
            var height = innerHeight;

            var move = false;

            function clear() {
                if (x > (width - 410) || x < 0) {
                    x_left = -x_left;
                }
                if (y > (height - 256) || y < 0) {
                    y_top = -y_top;
                }
                x = x + x_left;
                y = y + y_top;
                var x_px = x + "px";
                var y_px = y + "px";
                $("#img").css("left", x_px);
                $("#img").css("top", y_px);
            }

            var remove = setInterval(clear, 10);
            $("#move").click(function () {
                if (move) {
                    alert("shhsh");
                    remove = setInterval(clear, 10);
                    move = true;
                } else {
                    clearInterval(remove);
                    move = false;
                }

            });

        });
    </script>
</head>

<body>
![](img/2.jpg)<br/>
<input type="button" id="move" value="暂停" style="width: 60px;background-color: orange;"/>
</body>
</html>

相关文章

  • 图片上下左右飘动(jQuery+JS)

  • 下拉放大

    设置图片上下左右都拉伸 :zoomImageView?.contentMode = UIViewContentMo...

  • css 图片上下左右居中

  • PPT设计|图片组合与快速居中对齐

    1. 问题描述 PPT设计中往往需要插入图片排版,常规的展示中,我们往往希望图片将图片放置在版面的上下左右正中。手...

  • HTML+CSS \01

    1、如何实现不确定宽高的盒子上下左右居中 2、如何实现不确定宽高的图片上下左右居中 3、纯css写倒三角的原理:

  • css

    1、如何实现不确定宽高的盒子上下左右居中 2、如何实现不确定宽高的图片上下左右居中 3、纯css写倒三角的原理: ...

  • swift 含有图片和文字的button的布局

    Swift - 一句代码搞定button图片和文字布局 处理上下左右各种图片布局的button,一句代码解决。 代...

  • Github之UI总结

    (1)角标的消息个数(2)超强UIButton封装, 自定义图片上下左右位置, 自定义文字和图片间距 , 自定义角...

  • 插件的使用2

    Jquery图片无缝连续循环滚动 支持上下左右的滚动http://www.jq22.com/jquery-info...

  • RelavtiveLayout梅花布局,margin与paddi

    梅花布局:首先确定中心位置,其次根据中心位置的上下左右放置图片layout_toLeftOflayout_toRi...

网友评论

      本文标题:图片上下左右飘动(jQuery+JS)

      本文链接:https://www.haomeiwen.com/subject/gdkqextx.html