Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 背景中的滑动图像_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 背景中的滑动图像

Javascript 背景中的滑动图像,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有背景图像滑块。我想以非常平滑的方式滑动图像 body { /* Location of the image */ background-image: url(images/BACKGROUND1.jpg); /* Image is centered vertically and horizontally at all times */ background-position: center center; /* Image doesn't repeat */ b

我有背景图像滑块。我想以非常平滑的方式滑动图像

body {
  /* Location of the image */
  background-image: url(images/BACKGROUND1.jpg);


  /* Image is centered vertically and horizontally at all times */
  background-position: center center;

  /* Image doesn't repeat */
  background-repeat: no-repeat;

  /* Makes the image fixed in the viewport so that it doesn't move when 
     the content height is greater than the image height */
  background-attachment: fixed;

  /* This is what makes the background image rescale based on its container's size */
  background-size: cover;

  /* Pick a solid background color that will be displayed while the background image is loading */
  background-color:#464646;

  /* SHORTHAND CSS NOTATION
   * background: url(background-photo.jpg) center center cover no-repeat fixed;
   */
background-position:0px 33px;


   }

/* For mobile devices */
@media only screen and (max-width: 767px) {
  body {
    /* The file size of this background image is 93% smaller
     * to improve page load speed on mobile internet connections */
    background-image: url(images/BACKGROUND1.jpg);
  }
    }
 #b1 { background-image: url(images/BACKGROUND1.jpg); }
#b2 { background-image: url(images/BACKGROUND2 .jpg); }
#b3 { background-image: url(images/BACKGROUND3.jpg); }
#b4 { background-image: url(images/BACKGROUND4.jpg); }
#b5 { background-image: url(images/BACKGROUND1.jpg); }
#b6 { background-image: url(images/BACKGROUND2.jpg); }
#b7 { background-image: url(images/BACKGROUND3.jpg); }
#b8 { background-image: url(images/BACKGROUND4.jpg); }
#b9 { background-image: url(images/BACKGROUND3.jpg); }
#b10 { background-image: url(images/BACKGROUND1.jpg); }


    <html>
       <a href="#">next/</a>
         <a href="#">previuous/</a>

    </body>

</html>
正文{
/*图像的位置*/
背景图片:url(images/BACKGROUND1.jpg);
/*图像始终垂直和水平居中*/
背景位置:中心;
/*图像不会重复*/
背景重复:无重复;
/*使图像在视口中固定,以便在
内容高度大于图像高度*/
背景附件:固定;
/*这就是背景图像根据其容器大小重新缩放的原因*/
背景尺寸:封面;
/*选择加载背景图像时将显示的纯色背景色*/
背景色:#4646;
/*简写CSS符号
*背景:url(background photo.jpg)中盖无重复固定;
*/
背景位置:0px 33px;
}
/*用于移动设备*/
@仅介质屏幕和(最大宽度:767px){
身体{
/*此背景图像的文件大小小93%
*提高移动互联网连接上的页面加载速度*/
背景图片:url(images/BACKGROUND1.jpg);
}
}
#b1{背景图像:url(images/BACKGROUND1.jpg);}
#b2{背景图像:url(images/BACKGROUND2.jpg);}
#b3{背景图像:url(images/BACKGROUND3.jpg);}
#b4{背景图像:url(images/BACKGROUND4.jpg);}
#b5{背景图像:url(images/BACKGROUND1.jpg);}
#b6{背景图像:url(images/BACKGROUND2.jpg);}
#b7{背景图像:url(images/BACKGROUND3.jpg);}
#b8{背景图像:url(images/BACKGROUND4.jpg);}
#b9{背景图像:url(images/BACKGROUND3.jpg);}
#b10{背景图像:url(images/BACKGROUND1.jpg);}
我还想在单击时更改背景图像。我不是JavaScript方面的专家。有人能帮我吗?

试试看

这是一个简单的javascript框架,您可以使用它来满足您的需求


下次当你问任何问题时,请检查你键入的单词的拼写,并以简洁的方式解释

根据到目前为止的情况,您要做的是使用Javascript,以便它用数组中的下一个(b1到b10)替换指定单击时的主体id。这将处理背景图像之间的移动:

var body_ids = [
"b1",
"b2",
"b3",
"b4",
"b5",
"b6",
"b7",
"b8",
"b9",
"b10"
];
var current_body_id_index = 0;

var next_click = document.getElementById("click_next");
next_click.addEventListener("click", setNextBodyBackground);

var click_previous = document.getElementById("click_previous");
click_previous.addEventListener("click", setPreviousBodyBackground);


function setNextBodyBackground() {
  current_body_id_index++;
  if (current_body_id_index > 9) {
    current_body_id_index = 0;
  }
  document.body.id = body_ids[current_body_id_index];
}

function setPreviousBodyBackground() {
  current_body_id_index--;
  if (current_body_id_index < 0) {
    current_body_id_index = 9;
  }
  document.body.id = body_ids[current_body_id_index];
}
这会在每次更改背景时创建过渡效果。可以对此进行修改以创建不同的效果

我把这些都放进了一个例子

-webkit-transition: background 0.6s linear;
  -moz-transition: background 0.6s linear;
    -o-transition: background 0.6s linear;
      transition: background 0.6s linear;