【问题标题】:BXSlider with text - showing the fourth slide on load off-centre带有文本的 BXSlider - 显示负载偏离中心的第四张幻灯片
【发布时间】:2017-06-27 02:15:25
【问题描述】:

我正在使用 BXSlider 制作一个非常基本的文本滑块。我已经安装了它,但是滑块从最后一帧开始,并且在加载时偏离中心。当我更改幻灯片或移动块时,它会恢复正常。否则它工作正常。

到目前为止,我已经尝试更改 minSlides、maxSlides 和 slideWidth 设置,但这没有奏效。 maxSlides 确实更改了文本的位置,但我无法使其居中。

加载时:(链接已删除 - 需要更多声誉)。

与幻灯片互动后:http://imgur.com/cZQ9kCF.png

编辑:

仅当此插件:https://github.com/daneden/animate.css 为 body 类激活时才会出现问题。这是使用:

<body class="animated zoomInUp">

有什么方法可以暂停滑块启动直到动画结束?

【问题讨论】:

  • 在修复其余代码之前,您永远无法正确获取 bxSlider。您应该阅读以下内容:how to askminimal reproducible example。如果你能做到,我什至会帮你解决剩下的烂摊子。
  • 如果代码的其余部分有问题,如果您能指出我正确的方向,那就太好了。或者你的意思是我需要让它不那么凌乱?我感谢帮助文档 - 阅读它们后,我缩小了问题范围并更新了 OP。
  • 不是一个很好的minimal reproducible example,但你看起来对你的问题很认真。查看我的答案并记住我发布的代码将自行运行。如果您只是将其剪切并粘贴到 WordPress 或某些 CMS 等千篇一律的网站中,结果可能会有所不同。演示本身中有详细信息。在 CSS 中,cmets 是大写字母,但我没有大喊大叫,就是这样,所以很明显。如果答案有帮助记得点击✔接受答案。

标签: jquery html css bxslider


【解决方案1】:

更新 2

OP 需要更多的 JavaScript/jQuery 基础知识才能实现这些代码。为了加快这个对 OP 的解决方案,Demo 2 基本上是 Demo 1,但是合并在一个网页上。如果 Demo 2 被复制并粘贴到一个空白文本文件上,那么它的扩展名从 .txt 更改为 .html,然后在 现代浏览器中打开 *,它可以正常运行,无需额外要求。

*IE 不是一个正经的浏览器,它是 M$ 的一个小爱好,但它也应该在 IE 中运行。我添加的 JavaScript 完全是老派。

更新 1

OP 请求了一种延迟 bxSlider 实例化的方法,以便在加载页面时可以为主体设置动画。

  • 来自 animate.css 过渡持续时间的动画很慢(这是意料之中的,因为它看起来很适合缓动。)所以第一步是使用最新的加载事件:
window.onload 
  • 准备在window.onload 上加载的函数的最佳方法是将其包装在命名函数中。 重要语法:如果使用window.onload,必须去掉函数的括号
window.onload = init;
  • 尽管window.onload 是开始加载bxSlider 的最慢事件,但这还不够。所以我们需要添加一个setTimeout
 function init() {
      setTimeout(function() {
        var bx = $('.bxslider').bxSlider();
      }, 3500);
   }
   window.onload = init; 

详细信息在更新的演示中注释


该演示已更正了 HTML 和 CSS。使用原始代码,我怀疑 bxSlider 的修复是否有效。您需要真正熟悉一些 HTML 和 CSS 教程,或者至少使用 W3Schools 作为参考(他们有大量的教程和示例,但不是最好的。)

Plunker

演示1

function init() {
  setTimeout(function() {
    var bx = $('.bxslider').bxSlider();
  }, 3500);
}

window.onload = init;
@charset "utf-8";

/* DO NOT USE SO MANY FONT FAMILIES UNLESS YOU KNOW WHAT YOU ARE DOING*/


/* USE 2 AT THE MOST. ONE FOR TITLES AND ONE FOR NORMAL USE*/

@import url('https://fonts.googleapis.com/css?family=Nunito|PT+Sans:700');
* {
  margin: 0;
  padding: 0;
  border: 0;
}

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

body {
  font-family: "Nunito", sans-serif;
  font-size: 16px;
  overflow-x: hidden;
}

main {
  padding: 30px 0 0;
}

section {
  padding: 10px 20px;
}

p {
  line-height: 1.5;
  margin: 0 10px 10px;
}

li {
  list-style: none;
}

h1 {
  margin-top: 30px;
}

.slider {
  margin: 0 auto;
}

[class*='title'] {
  font-family: "PT Sans";
  text-align: center;
  color: #FFF;
  background-color: #00aee8;
}

.title {
  font-size: 48px;
}

.subtitle {
  background-color: #ef233c;
  font-size: 24px;
  margin: 15px;
}

.boxtext {
  background-color: #ef233c;
  font-size: 32px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.bodytext {
  font-size: 18px;
  color: #160C28;
}

.btctitle {
  font-size: 48px;
  color: #000;
  background-color: #FF3;
}

footer {
  padding: 10px 5px 40px;
  margin-top: 0;
}

footer p {
  text-align: center;
  font-size: 18px;
}

a,
a:link,
a:visited {
  position: relative;
  color: #fff;
  text-decoration: none;
}

a:hover,
a:active,
a:focus {
  color: transparent;
  background-color: transparent;
  text-shadow: -.25px -.25px 0 #fc0, -.25px .25px 0 #fc0, .25px -.25px 0 #fc0, .25px .25px 0 #fc0;
}

a.x::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 2px;
  bottom: 0;
  left: 0;
  background-color: #fc0;
  visibility: hidden;
  transform: scaleX(0);
  transition: all .5s ease-in-out 0s;
}

a.x:hover::before,
a.x:active::before,
a.x:focus::before {
  visibility: visible;
  transform: scaleX(1);
}

.btcbutton {
  font-size: 48px;
  background: rgba(255, 200, 12, 1);
}


/* THE LAST TWO RULESETS IS THE ANSWER BUT WILL MOST LIKELY
|| NOT WORK CORRECTLY WITH THE ORIGINAL CSS and HTML. THE
|| PADDING MIN-HEIGHT IS BY PREFERENCE
*/

.bx-wrapper {
  margin: 0 auto;
  min-height: 100px;
}

.bxslider li {
  padding: 0 55px;
  margin: 0 auto;
  text-align: center;
  color: red;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Practical Applications</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/animatecss/3.5.2/animate.min.css">
  <link href="CSS.css" rel="stylesheet">
  <link href='https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css' rel='stylesheet'>
  <style>

  </style>
</head>

<body class="animated zoomInUp">
  <header>
    <h1>
      <a href="index.html">
        <img src="https://wemakewebsites.com/sites/default/files/styles/large/public/post_images/headline/bitcoin-logo-with-padding.png?itok=mnfN-yoC" width="100%" height="100">
      </a>
    </h1>

    <nav>
      <ul class='boxtext'>

        <li class="subtitle"><a href="How.html" title="How does it work?" class='x'>How?</a></li>
        <li class="subtitle"><a href="Why.html" title="Why use a blockchain?" class='x'>Why?</a></li>
        <li class="subtitle"><a href="Practical_Applications.html" title="Practical Applications" class='x'>What?</a></li>

      </ul>
    </nav>


    <h2 class="title">Practical Applications</h2>
  </header>
  <main>
    <section class="bodytext">
      <p>So far, blockchains have been as a ledger of transactions. Cryptocurrencies, as they are termed, are favoured as transactions are stored across the blockchain. This make it impossible for a government or a bank to interfere with transaction history,
        and it's almost impossible for the blockchain to be compromised or lost like a bank database could be.</p>
      <p>Here are some problems that you need to be aware of:</p>

      <!-- slider -->
    </section>
    <section class='slider'>
      <ul class="bxslider">
        <li class="bodytext">
          You obviously put some effort into this code. Why not put some effort into learning how to do it simple first. Every style you add to the stylesheet compounds your problem.

        </li>
        <li class="bodytext">
          Don't use table, tr, and th for layout. You didn't add an end tag for h1. center tags are not supported in HTML5. id='top' is an id, id must be unique. id='top' is used on 3 different tags. Do not do that. Change it to class='top'.

        </li>
        <li class="bodytext">
          CSS link tags must be before any script tags. Script tags should be placed before the closing end tag of body. Do not use position:absolute without wrapping it in element that has position:relative.

        </li>
        <li class="bodytext">
          CSS.css file is commented with corrections. You need to know the fundamentals, Google search: "CSS tutorial"

        </li>
        <li class="bodytext">
          At the beginning of page, only !doctype html is needed for HTML5. Why would you use HTML4? You need to know the fundamentals, Google search: "HTML5 tutorial"

        </li>
      </ul>
    </section>
  </main>
  <footer>
    <p>Let's explore the most well-known utilisation of blockchain technology to date:</p>

    <p>
      <a href="Bitcoin.html" class="btcbutton x"> <span>Bitcoin</span>
      </a>
    </p>
  </footer>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
  <script src='https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.min.js'></script>

</body>

</html>

演示 2

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Practical Applications</title>
  <link href="https://cdn.jsdelivr.net/animatecss/3.5.2/animate.min.css" rel="stylesheet">
  <link href='https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css' rel='stylesheet'>
  <link href="https://fonts.googleapis.com/css?family=Nunito|PT+Sans:700" rel="stylesheet">
  <style>
    * {
      margin: 0;
      padding: 0;
      border: 0;
    }
    
    html,
    body {
      width: 100%;
      height: 100%
    }
    
    body {
      font-family: "Nunito", sans-serif;
      font-size: 16px;
      overflow-x: hidden;
    }
    
    main {
      padding: 30px 0 0;
    }
    
    section {
      padding: 10px 20px;
    }
    
    p {
      line-height: 1.5;
      margin: 0 10px 10px;
    }
    
    li {
      list-style: none;
    }
    
    h1 {
      margin-top: 30px;
    }
    
    .slider {
      margin: 0 auto;
    }
    
    [class*='title'] {
      font-family: "PT Sans";
      text-align: center;
      color: #FFF;
      background-color: #00aee8;
    }
    
    .title {
      font-size: 48px;
    }
    
    .subtitle {
      background-color: #ef233c;
      font-size: 24px;
      margin: 15px;
    }
    
    .boxtext {
      background-color: #ef233c;
      font-size: 32px;
      display: flex;
      justify-content: space-around;
      align-items: center;
    }
    
    .bodytext {
      font-size: 18px;
      color: #160C28;
    }
    
    .btctitle {
      font-size: 48px;
      color: #000;
      background-color: #FF3;
    }
    
    footer {
      padding: 10px 5px 40px;
      margin-top: 0;
    }
    
    footer p {
      text-align: center;
      font-size: 18px;
    }
    
    a,
    a:link,
    a:visited {
      position: relative;
      color: #fff;
      text-decoration: none;
    }
    
    a:hover,
    a:active,
    a:focus {
      color: transparent;
      background-color: transparent;
      text-shadow: -.25px -.25px 0 #fc0, -.25px .25px 0 #fc0, .25px -.25px 0 #fc0, .25px .25px 0 #fc0;
    }
    
    a.x::before {
      content: "";
      position: absolute;
      width: 100%;
      height: 2px;
      bottom: 0;
      left: 0;
      background-color: #fc0;
      visibility: hidden;
      transform: scaleX(0);
      transition: all .5s ease-in-out 0s;
    }
    
    a.x:hover::before,
    a.x:active::before,
    a.x:focus::before {
      visibility: visible;
      transform: scaleX(1);
    }
    
    .btcbutton {
      font-size: 48px;
      background: rgba(255, 200, 12, 1);
    }
    
    .bx-wrapper {
      margin: 0 auto;
      min-height: 100px;
    }
    
    .bxslider li {
      padding: 0 55px;
      margin: 0 auto;
      text-align: center;
      color: red;
    }
  </style>
</head>

<body>
  <header>
    <h1 class="animated zoomInUp">
      <a href="index.html">
        <img src="https://wemakewebsites.com/sites/default/files/styles/large/public/post_images/headline/bitcoin-logo-with-padding.png?itok=mnfN-yoC" width="100%" height="100">
      </a>
    </h1>

    <nav>
      <ul class='boxtext'>

        <li class="subtitle"><a href="How.html" title="How does it work?" class='x'>How?</a></li>
        <li class="subtitle"><a href="Why.html" title="Why use a blockchain?" class='x'>Why?</a></li>
        <li class="subtitle"><a href="Practical_Applications.html" title="Practical Applications" class='x'>What?</a></li>

      </ul>
    </nav>


    <h2 class="title">Practical Applications</h2>
  </header>
  <main>
    <section class="bodytext">
      <p>So far, blockchains have been as a ledger of transactions. Cryptocurrencies, as they are termed, are favoured as transactions are stored across the blockchain. This make it impossible for a government or a bank to interfere with transaction history,
        and it's almost impossible for the blockchain to be compromised or lost like a bank database could be.</p>
      <p>Here are some problems that you need to be aware of:</p>

      <!-- slider -->
    </section>
    <section class='slider'>
      <ul class="bxslider">
        <li class="bodytext">
          You obviously put some effort into this code. Why not put some effort into learning how to do it simple first. Every style you add to the stylesheet compounds your problem.

        </li>
        <li class="bodytext">
          Don't use table, tr, and th for layout. You didn't add an end tag for h1. center tags are not supported in HTML5. id='top' is an id, id must be unique. id='top' is used on 3 different tags. Do not do that. Change it to class='top'.

        </li>
        <li class="bodytext">
          CSS link tags must be before any script tags. Script tags should be placed before the closing end tag of body. Do not use position:absolute without wrapping it in element that has position:relative.

        </li>
        <li class="bodytext">
          CSS.css file is commented with corrections. You need to know the fundamentals, Google search: "CSS tutorial"

        </li>
        <li class="bodytext">
          At the beginning of page, only !doctype html is needed for HTML5. Why would you use HTML4? You need to know the fundamentals, Google search: "HTML5 tutorial"

        </li>
      </ul>
    </section>
  </main>
  <footer>
    <p>Let's explore the most well-known utilisation of blockchain technology to date:</p>

    <p>
      <a href="Bitcoin.html" class="btcbutton x"> <span>Bitcoin</span>
      </a>
    </p>
  </footer>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
  <script src='https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.min.js'></script>
  <script>
    function init() {
      setTimeout(function() {
        var bx = $('.bxslider').bxSlider();
      }, 3500);
    }

    window.onload = init;
  </script>
</body>

</html>

【讨论】:

  • 非常感谢您花时间重做代码。因为我误用了 h1 标签而不是更改 body 类,所以我没有说清楚的是,整个页面应该具有动画效果。是的,我知道这是一个糟糕的用户体验决定。我可以通过将主体类更改为“动画 zoomInUp”来使用您的代码复制该问题。使用“动画反弹”之类的不同效果很好。所以我的信念是我的代码不是原因,但是使用这种效果会破坏加载时的滑块,这就是为什么我问是否有某种方法可以暂停它,直到页面“放大”。
  • @user16421 知道了,请参阅更新。效果出奇的好。
  • 再次感谢。还有一件事——上面的 sn-p 是如何在顶部实现单独的 js 的?要让它在我的机器上运行,我是否需要将它作为文档顶部的外部 scripts.js 文件引用,以便它首先运行或在其他地方的
  • @user16421 阅读更新 2 以使演示 2 在 sn-p 之外运行。 不要更改任何内容,只需按照更新 2 中的说明复制并粘贴整个代码。在浏览器中运行单个页面后,您应该检查代码并记下位置: 1. &lt;link&gt; 准确放置 2. &lt;style&gt; 准确放置 3. &lt;script src=....&gt; 准确放置 b> 4. `
  • 如果没有,那么您需要详细说明您在主机中的位置,或者您是否只是使用本地主机,您使用的是什么类型的计算机等。
【解决方案2】:

这是一个特别影响 chrome 的错误,我通常可以通过以下方式修复:

.bx-viewport li {
    min-height: 1px;
    min-width: 1px;
}

我在我在 bxslider css 文件之后链接的样式表文件中覆盖它,就像我在 https://kensetsutenshokunavi.jp/ 中所做的那样。在这种情况下,我还隐藏了滑块,直到加载图像,因为它很重并且由于某种原因破坏了布局。

如果可能的话,我还建议给幻灯片容器提供宽度和/或高度。如果不检查您的代码,我无法提出任何进一步的建议。无论如何,我希望它有所帮助。

【讨论】:

  • 我在 Chrome、IE 和 Firefox 上的修复仍然存在问题。 Safari 工作正常。没有改变也是如此。这是CSS应该看起来的样子吗?默认情况下我没有 .bx-viewport li imgur.com/kWRyyZE
  • 感谢您的帮助。我尝试了覆盖 CSS 并没有发生任何事情。然后我为我的 div 创建了一个新类来设置宽度/高度 - 没有修复它,但奇怪的是高度设置没有改变滑块的高度,但宽度设置却改变了。这是代码,对不起,我真的不知道我在做什么。 HTML:pastebin.com/XGU6ZfFw CSS(底部的相关内容):pastebin.com/HiRG3wgi
  • 我已经缩小了问题范围并更新了 OP - 无需浪费您的时间筛选该代码。
猜你喜欢
  • 2013-08-24
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多