Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.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_Html_Css_Dom_Slideshow - Fatal编程技术网

幻灯片/旋转木马Javascript在外部文件中不工作

幻灯片/旋转木马Javascript在外部文件中不工作,javascript,html,css,dom,slideshow,Javascript,Html,Css,Dom,Slideshow,一般来说,我是编程新手,如果我的问题没有建设性和明确性,我深表歉意,但我花了时间,希望能将问题形象化。我测试了Javascript,想要一个带有基本Javascript的imageslider。 但是,当javascript写在外部工作表中时,我不明白为什么我的代码不会起作用 当页面加载时,imageslider最初不会显示,但在HTML文件中写入时它可以工作 下面是示例中的代码 这是你的电话号码 javascript: 请检查标签的放置位置,如果标签位于中,请将其放置在的正上方,这样可以解决您

一般来说,我是编程新手,如果我的问题没有建设性和明确性,我深表歉意,但我花了时间,希望能将问题形象化。我测试了Javascript,想要一个带有基本Javascript的imageslider。 但是,当javascript写在外部工作表中时,我不明白为什么我的代码不会起作用

当页面加载时,imageslider最初不会显示,但在HTML文件中写入时它可以工作

下面是示例中的代码

这是你的电话号码

javascript:


请检查
标签的放置位置,如果标签位于
中,请将其放置在
的正上方,这样可以解决您的问题

请参考此代码:

script.js

var slideIndex=1;
放映幻灯片(幻灯片索引);
函数加滑块(n){
放映幻灯片(幻灯片索引+=n);
}
功能当前幻灯片(n){
放映幻灯片(幻灯片索引=n);
}
功能演示幻灯片(n){
var i;
var slides=document.getElementsByClassName(“mySlides”);
var dots=document.getElementsByClassName(“dot”);
如果(n>幻灯片长度){
slideIndex=1
}
if(n<1){
slideIndex=slides.length
}
对于(i=0;i
index.html


样品
* {
框大小:边框框
}
身体{
字体系列:Verdana,无衬线;
保证金:0
}
迈斯利德斯先生{
显示:无
}
img{
垂直对齐:中间对齐;
}
/*幻灯片容器*/
.slideshow容器{
最大宽度:1000px;
位置:相对位置;
保证金:自动;
}
/*下一步和上一步按钮*/
.prev,
.下一个{
光标:指针;
位置:绝对位置;
最高:50%;
宽度:自动;
填充:16px;
利润上限:-22px;
颜色:白色;
字体大小:粗体;
字号:18px;
过渡:0.6s缓解;
边界半径:0 3px 3px 0;
用户选择:无;
}
/*将“下一步按钮”置于右侧*/
.下一个{
右:0;
边界半径:3px 0 3px;
}
/*在悬停时,添加一个黑色背景色,带有一点透明*/
.prev:悬停,
.下一步:悬停{
背景色:rgba(0,0,0,0.8);
}
/*标题文本*/
.文本{
颜色:#F2F2;
字体大小:15px;
填充:8px 12px;
位置:绝对位置;
底部:8px;
宽度:100%;
文本对齐:居中;
}
/*数字文本(1/3等)*/
.numbertext{
颜色:#F2F2;
字体大小:12px;
填充:8px 12px;
位置:绝对位置;
排名:0;
}
/*点/子弹/指示器*/
多特先生{
光标:指针;
高度:15px;
宽度:15px;
边际:0.2px;
背景色:#bbb;
边界半径:50%;
显示:内联块;
过渡:背景色0.6s;
}
.主动,
.dot:悬停{
背景色:#717171;
}
/*褪色动画*/
.褪色{
-webkit动画名称:淡入淡出;
-webkit动画持续时间:1.5s;
动画名称:淡入淡出;
动画持续时间:1.5s;
}
@-webkit关键帧淡入淡出{
从{
不透明度:.4
}
到{
不透明度:1
}
}
@关键帧淡入淡出{
从{
不透明度:.4
}
到{
不透明度:1
}
}
/*在较小的屏幕上,减小文本大小*/
@仅介质屏幕和(最大宽度:300px){
.prev,
下一个
.文本{
字号:11px
}
}
1/3
标题文本
2/3
标题二
3/3
标题三

请检查您的
标签的放置位置,如果标签位于
中,请将其放置在
的正上方,这样可以解决您的问题

请参考此代码:

script.js

var slideIndex=1;
放映幻灯片(幻灯片索引);
函数加滑块(n){
放映幻灯片(幻灯片索引+=n);
}
功能当前幻灯片(n){
放映幻灯片(幻灯片索引=n);
}
功能演示幻灯片(n){
var i;
var slides=document.getElementsByClassName(“mySlides”);
var dots=document.getElementsByClassName(“dot”);
如果(n>幻灯片长度){
slideIndex=1
}
if(n<1){
slideIndex=slides.length
}
对于(i=0;i
index.html


样品
* {
框大小:边框框
}
身体{
字体系列:Verdana,无衬线;
保证金:0
}
迈斯利德斯先生{
显示:无
}
img{
垂直
var slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " active";
}
<script src="js/slider.js"></script>
Uncaught TypeError: Cannot read property 'style' of undefined
at showSlides (slider.js:27)
at slider.js:3`

showSlides(slideIndex);    This shows error   
slides[slideIndex-1].style.display = "block";   This shows error 
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
    showSlides(slideIndex += n);
}

function currentSlide(n) {
    showSlides(slideIndex = n);
}

function showSlides(n) {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    var dots = document.getElementsByClassName("dot");
    if (n > slides.length) {
        slideIndex = 1
    }
    if (n < 1) {
        slideIndex = slides.length
    }
    for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
    }
    for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
    }
    slides[slideIndex - 1].style.display = "block";
    dots[slideIndex - 1].className += " active";
}
<!DOCTYPE html>
<html>
   <head>
      <title>Sample</title>
      <style> 
         * {
            box-sizing: border-box
         }
         body {
            font-family: Verdana, sans-serif;
            margin: 0
         }
         .mySlides {
            display: none
         }
         img {
            vertical-align: middle;
         }
         /* Slideshow container */
         .slideshow-container {
            max-width: 1000px;
            position: relative;
            margin: auto;
         }
         /* Next & previous buttons */
         .prev,
         .next {
            cursor: pointer;
            position: absolute;
            top: 50%;
            width: auto;
            padding: 16px;
            margin-top: -22px;
            color: white;
            font-weight: bold;
            font-size: 18px;
            transition: 0.6s ease;
            border-radius: 0 3px 3px 0;
            user-select: none;
         }
         /* Position the "next button" to the right */
         .next {
            right: 0;
            border-radius: 3px 0 0 3px;
         }
         /* On hover, add a black background color with a little bit see-through */
         .prev:hover,
         .next:hover {
            background-color: rgba(0, 0, 0, 0.8);
         }
         /* Caption text */
         .text {
            color: #f2f2f2;
            font-size: 15px;
            padding: 8px 12px;
            position: absolute;
            bottom: 8px;
            width: 100%;
            text-align: center;
         }
         /* Number text (1/3 etc) */
         .numbertext {
            color: #f2f2f2;
            font-size: 12px;
            padding: 8px 12px;
            position: absolute;
            top: 0;
         }
         /* The dots/bullets/indicators */
         .dot {
            cursor: pointer;
            height: 15px;
            width: 15px;
            margin: 0 2px;
            background-color: #bbb;
            border-radius: 50%;
            display: inline-block;
            transition: background-color 0.6s ease;
         }
         .active,
         .dot:hover {
            background-color: #717171;
         }
         /* Fading animation */
         .fade {
            -webkit-animation-name: fade;
            -webkit-animation-duration: 1.5s;
            animation-name: fade;
            animation-duration: 1.5s;
         }
         @-webkit-keyframes fade {
           from {
             opacity: .4
           }
           to {
             opacity: 1
           }
         }
         @keyframes fade {
           from {
             opacity: .4
           }
           to {
             opacity: 1
           }
         }
         /* On smaller screens, decrease text size */
         @media only screen and (max-width: 300px) {
         .prev,
         .next,
         .text {
            font-size: 11px
           }
         }
      </style>
   </head>
   <body>
      <div class="slideshow-container">
         <div class="mySlides fade">
            <div class="numbertext">1 / 3</div>
            <img src="img_nature_wide.jpg" style="width:100%">
            <div class="text">Caption Text</div>
         </div>
         <div class="mySlides fade">
            <div class="numbertext">2 / 3</div>
            <img src="img_snow_wide.jpg" style="width:100%">
            <div class="text">Caption Two</div>
         </div>
         <div class="mySlides fade">
            <div class="numbertext">3 / 3</div>
            <img src="img_mountains_wide.jpg" style="width:100%">
            <div class="text">Caption Three</div>
         </div>
         <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
         <a class="next" onclick="plusSlides(1)">&#10095;</a>
      </div>
      <br>
      <div style="text-align:center">
         <span class="dot" onclick="currentSlide(1)"></span> 
         <span class="dot" onclick="currentSlide(2)"></span> 
         <span class="dot" onclick="currentSlide(3)"></span> 
      </div>
      <script src="script.js"></script>
   </body>
</html>