javascript - 页面内样式标签内的 CSS 加载,但不从外部样式表加载

标签 javascript jquery html css wordpress

我正在使用 CSS 技巧中的这种滑入效果。部分使用visible插件,动画通过CSS实现。

http://css-tricks.com/slide-in-as-you-scroll-down-boxes/

当 CSS 在页面内加载样式标签时它起作用,但是当我从外部样式表加载它时它不起作用。我已经检查过样式表是否正在加载,确实如此。我想从外部加载它以将我的 CSS 保存在一个文件中。我试过让它在 HTML 和 wordpress 中工作,但得到相同的结果。我使用 HTML5 重置文件。谢谢,蒂姆。

我试过在样式标签中使用:@import url("slidein.css"); 但没有用。

在 Wordpress 中,我在外部文件中加载 javascript,即。

<script src="<?php echo get_template_directory_uri(); ?>/js/index.js"></script>

在 HTML 中:

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

Wordpress 中的 CSS:

<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/slidein.css" />

和 HTML:

<link rel="stylesheet" type="text/css "href="assets/css/slidein.css" />

index.js 中的 javascript:

$.fn.offScreen = function(distance){

        var $t              = $(this),
            $w              = $(window),
            viewTop         = $w.scrollTop(),
            viewBottom      = viewTop + $w.height(),
            _top            = $t.offset().top - distance,
            _bottom     = $t.offset().top + $t.height() + distance;

   return {
     top: _bottom <= viewTop,
     bottom: _top >= viewBottom
   }

};

var win = $(window);

var allMods = $(".module");

allMods.each(function(i, el) {
  var el = $(el);
  if (!el.offScreen(150).bottom) {
    el.addClass("already-visible"); 
  } 
});

win.on("scroll resize",function(event) 
{

  allMods.each(function(i, el) {
    var el = $(el);
    if (!el.offScreen(150).top && !el.offScreen(150).bottom) 
    {
      el.removeClass("already-visible off-screen-top off-screen-bottom"); 
      el.addClass("come-in"); 
    } 
    else
    {

        if(el.offScreen(150).top)
        {
            el.addClass("off-screen-top"); 
        }
        else
        {
            el.addClass("off-screen-bottom"); 
        }
    }
  });//allMods.each()

});//win.scroll()

win.trigger("scroll");

CSS:

* {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

section {
  background: #eee;
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  overflow: hidden;
}

.module {
  width: 48%;
  min-height: 200px;
  background: white;
  position: relative;
  float: left;
  padding: 20px;
  margin-right: 4%;
  margin-bottom: 4%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transform-origin: center center;
}
.module:nth-child(even) {
  margin-right: 0;
}
.module.off-screen-top {
  transform: translateY(-150px);
}
.module.off-screen-bottom {
  transform: translateY(150px);
}

body {
  background: url(http://s.cdpn.io/3/blurry-blue.jpg);
  background-size: cover;
  padding: 30px;
}

.come-in {
  transform: translateY(0);
  transition: transform 0.8s ease-out;
}

.come-in:nth-child(odd) {
  transition-duration: 0.5s;
}

.already-visible {
  transform: translateY(0);
  transition: none;
}
@keyframes come-in {
  to {
    transform: translateY(0);
  }
}

最佳答案

在 WordPress 中,您应该使用 wp_enqueue_style() 将您的外部样式表加入队列,确保 Hook 到 wp_enqueue_scripts。例如:

function my_enqueue_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
}

add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );

引用:http://codex.wordpress.org/Function_Reference/wp_enqueue_style

关于javascript - 页面内样式标签内的 CSS 加载,但不从外部样式表加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820529/

相关文章:

javascript - 调整 G 标签内的 SVG 矩形元素的大小

javascript - 将 Google Admin SDK 的响应(包含大括号字符串和项目)转换为多维数组

javascript - 如何使用 Jquery 选择内联文本?

Javascript/JQuery 遍历数组值

html - 调整 html 复选框的大小以用于 pdf 输出

javascript - DataTables 设置默认排序列并设置不可排序列

javascript - ExtJs标签不显示

javascript - 使用 ajax 从 db 获取位置更改我的图像 src

javascript - 无法从开放天气 api 访问 JSON

javascript - 如何使用预先写入的数据制作 html &lt;input&gt; 标签