【问题标题】:Background Images changing with a click背景图像通过点击改变
【发布时间】:2019-10-22 22:21:45
【问题描述】:

我正在尝试更改 mouseclick 上的 4 张背景图片。

目前,同一图像在屏幕上出现 4 次(每个角落 1 次)。 当我单击当前图像时,我希望整个屏幕上只出现一张图像,而其他图像则替换它;当您再次单击时,依此类推。 我不确定我的代码目前有什么问题。

HTML:

<!DOCTYPE html>
<html lang="fr" dir="ltr">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">

  <title> CODE </title>
  <link rel="stylesheet" type="text/css" href="css/style.css"/>
  <script src="js/jquery.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <script src="js/sketch.js"></script>

</head>

<body>

<div id="body" class="imageOne"></div>

</body>

</html>

JS:

var bodyObj,
  className,
  index;

bodyObj = document.getElementById('body');
index = 1;
className = [
  'imageOne',
  'imageTwo'
];

function updateIndex() {
  if (index === 0) {
    index = 1;
  } else {
    index = 0;
  }
}

bodyObj.onclick = function(e) {
  e.currentTarget.className = className[index];
  updateIndex();
}

CSS:

html, body, #body {
    height: 100%;
    width: 100%;
}

#body.imageOne {
    background-image: url("img1.png");
}

#body.imageTwo {
    background-image: url("img2.png");
}

#body.imageTwo {
    background-image: url("img3.png");
}

#body.imageTwo {
    background-image: url("img4.png");
}

【问题讨论】:

    标签: javascript html onclick addeventlistener mouseclick-event


    【解决方案1】:

    与其对每个图像使用类,不如将它们存储在一个数组中,然后以编程方式更改它。请在下面找到sn-p。

    let imgList = [
      'https://dummyimage.com/200x200/000/fff&text=image+1',
      'https://dummyimage.com/200x200/000/fff&text=image+2', 'https://dummyimage.com/200x200/000/fff&text=image+3', 'https://dummyimage.com/200x200/000/fff&text=image+4'
    ];
    
    let currentIndex = 0;
    
    function changeImg() {
      $('#body').css('backgroundImage', `url(${imgList[currentIndex]})`);
      currentIndex++;
      if (currentIndex == imgList.length) currentIndex = 0;
    }
    
    changeImg();
    
    $('#body').on('click', changeImg);
    #body {
      width: 200px;
      height: 200px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="body" class="imageOne"></div>

    【讨论】:

      【解决方案2】:

      这是另一种方式(像你一样使用类):

      var bodyObj   = document.getElementById('body'),
          index     = 0,
          className = [
            'imageOne',
            'imageTwo',
            'imageThree',
            'imageFour'
          ];
      
      bodyObj.onclick = function(e) {
        index = (index + 1) % className.length;
        e.currentTarget.className = className[index];
      }
      html,body,#body { margin: 0; height: 100%; width: 100%; }
      
      #body { background-position: center; background-size: cover; }
      
      #body.imageOne { background-image: url("https://picsum.photos/id/9/536/354"); }
      #body.imageTwo { background-image: url("https://picsum.photos/id/3/536/354"); }
      #body.imageThree { background-image: url("https://picsum.photos/id/1/536/354"); }
      #body.imageFour { background-image: url("https://picsum.photos/id/2/536/354"); }
      &lt;div id="body" class="imageOne"&gt;&lt;/div&gt;

      【讨论】:

        【解决方案3】:

        我试过这个效果很好!

        var index = 0;
        var className = ['imageOne',
        'imageTwo',
        'imageThree',
        'imageFour',
        'imageFive'];
        
        $(document).ready(function() {
        $("#main").on("click", function() {
        index = (index + 1) % className.length;
        $(this).attr('class', className[index]);
        });
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 2017-08-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多