Google Script HTML / CSS / JS代码未运行

时间:2018-04-04 20:11:00

标签: javascript html css google-apps-script owl-carousel

我有一个在我的本地机器上工作的自举旋转木马,但是当我尝试在Google Scripts环境中实施时,它没有工作。

我已按照本网站的说明进行操作,但似乎根本无法工作:https://developers.google.com/apps-script/guides/html/best-practices。事实上,当我遵循这些指导原则时,它只显示3个点(我的HTML的一部分),而不是其他任何内容。当我拿出本指南建议的代码时,它会显示我的HTML,但无法与CSS或JS代码集成。

这是我的代码:



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++) {
    // clearInterval( timer );
    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";
}

// Sets transition every 3 seconds
timer = setInterval( function(){
  plusSlides(1); }, 3000 );
&#13;
* {
  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;
}

/* 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 */
.carousel-caption {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;

  border: none;
  background-color: rgb(0,0,0,.6);

}

/* 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}
}

/* Transition right to left */
&#13;
<head>
  <base target="_top">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
  <div class="slideshow-container">
  <div class="mySlides">
    <div class="numbertext">1 / 5</div>
    <img src="https://static.pexels.com/photos/236047/pexels-photo-236047.jpeg" style="width:100%">
    <div class="carousel-caption">
      <h3>Caption 1</h3>
      <p>Caption 1 text</p>
    </div>
  </div>

  <div class="mySlides">
    <div class="numbertext">2 / 5</div>
    <img src="https://www.nature.org/cs/groups/webcontent/@web/documents/media/2016-photocontest-yosemite-w-1.jpg" style="width:100%">
    <div class="carousel-caption">
      <h3>Caption 2</h3>
      <p>Caption 2 text</p>
    </div>
  </div>

  <div class="mySlides">
    <div class="numbertext">3 / 5</div>
    <img src="https://static.pexels.com/photos/33109/fall-autumn-red-season.jpg" style="width:100%">
    <div class="carousel-caption">
      <h3>Caption 2</h3>
      <p>Caption 2 text</p>
    </div>
  </div>

<!-- ADD ADDITIONAL IMAGES IN THE [ADD IMG ...] PART -->
  <!-- <div class="mySlides fade">
    <div class="numbertext">4 / 5</div>
    <img src="[ADD IMG ...]" style="width:100%">
    <div class="text">This is for a Caption text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">5 / 5</div>
    <img src="[ADD IMG] ..." style="width:100%">
    <div class="text">This is for a Caption text</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>
    <!-- <span class="dot" onclick="currentSlide(4)"></span>
    <span class="dot" onclick="currentSlide(5)"></span>  -->
  </div>

</body>


<script type="text/javascript" src="interactions.js"> </script>
<script src="js/jquery-1.7.1.min.js"></script>
<!-- <script src="js/bootstrap.js"></script> -->
&#13;
&#13;
&#13;

这是我的code.gs:

function doGet() {
  return HtmlService.createTemplateFromFile('Index')
      .evaluate();
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

谢谢!

2 个答案:

答案 0 :(得分:1)

你的代码失败了,因为你没有jQuery。将其添加到您的项目中。订单很重要!

<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/yourscript.js"></script>

答案 1 :(得分:0)

应该在调用$之前加载jQuery。此外,代码错过了调用一些owlCarousel重要文件。

我从http://owlcarousel2.github.io/OwlCarousel2/借了丢失的文件,并注释掉了以下几行

$('.slideshow-container').owlCarousel({
  rtl: true
});

上面的代码显然在Stack Snippet上工作正常。

&#13;
&#13;
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++) {
    // clearInterval( timer );
    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";
}

// Sets transition every 3 seconds
timer = setInterval(function() {
  plusSlides(1);
}, 3000);

/*
$('.slideshow-container').owlCarousel({
  rtl: true
});
*/
&#13;
* {
  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;
}


/* 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 */

.carousel-caption {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
  border: none;
  background-color: rgb(0, 0, 0, .6);
}


/* 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
  }
}


/* Transition right to left */
&#13;
<!-- Owl Stylesheets -->
<link rel="stylesheet" href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css">
<link rel="stylesheet" href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css">

<!-- JavaScript files -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js"></script>

<body>
  <div class="slideshow-container">
    <div class="mySlides">
      <div class="numbertext">1 / 5</div>
      <img src="https://static.pexels.com/photos/236047/pexels-photo-236047.jpeg" style="width:100%">
      <div class="carousel-caption">
        <h3>Caption 1</h3>
        <p>Caption 1 text</p>
      </div>
    </div>

    <div class="mySlides">
      <div class="numbertext">2 / 5</div>
      <img src="https://www.nature.org/cs/groups/webcontent/@web/documents/media/2016-photocontest-yosemite-w-1.jpg" style="width:100%">
      <div class="carousel-caption">
        <h3>Caption 2</h3>
        <p>Caption 2 text</p>
      </div>
    </div>

    <div class="mySlides">
      <div class="numbertext">3 / 5</div>
      <img src="https://static.pexels.com/photos/33109/fall-autumn-red-season.jpg" style="width:100%">
      <div class="carousel-caption">
        <h3>Caption 2</h3>
        <p>Caption 2 text</p>
      </div>
    </div>

    <!-- ADD ADDITIONAL IMAGES IN THE [ADD IMG ...] PART -->
    <!-- <div class="mySlides fade">
    <div class="numbertext">4 / 5</div>
    <img src="[ADD IMG ...]" style="width:100%">
    <div class="text">This is for a Caption text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">5 / 5</div>
    <img src="[ADD IMG] ..." style="width:100%">
    <div class="text">This is for a Caption text</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>
    <!-- <span class="dot" onclick="currentSlide(4)"></span>
    <span class="dot" onclick="currentSlide(5)"></span>  -->
  </div>

</body>



<!-- <script src="js/bootstrap.js"></script> -->
&#13;
&#13;
&#13;