简体   繁体   English

动态背景色变化

[英]dynamic background-color change

I work on my portfolio website, it's based on a fullpage.js and WordPress. 我在我的投资组合网站上工作,它基于fullpage.js和WordPress。 I would like to have fixed background with changing custom "background-color". 我想通过更改自定义“背景颜色”来固定背景。

  • I add different "background-color" to every post with a custom field. 我为每个带有自定义字段的帖子添加了不同的“背景色”。

  • I change class name for visible section (post) inside a full page 我在整页中更改可见部分(帖子)的类名
    script, visible post gets class "active". 脚本,可见帖子获取类“活动”。

I know that fullpage.js doesn't allow fixed elements, so I have to put some "background" div outside of full page wrapper (and WordPress loop too). 我知道fullpage.js不允许使用固定元素,因此我必须在整个页面包装程序(以及WordPress循环)之外放置一些“背景” div。 I don't know how to copy that "background-color" from a visible (class "active") section to a fixed background in a separate div. 我不知道如何将“背景色”从可见(“活动”类)部分复制到单独div中的固定背景。 There are some possibilities: 有一些可能性:

  • use js/jQuery to copy CSS "background-color" value from one ( class "active") div to another(fixed background div), but it has to be done dynamically. 使用js / jQuery将CSS“背景颜色”值从一个(类“活动”)div复制到另一个(固定背景div),但是必须动态完成。 How? 怎么样? All scripts I found and tested works only once for first "active" background-color, and when section change (another div is "active") fixed background still has the first active section background-color. 我找到并测试的所有脚本仅对第一个“活动”背景色起作用一次,并且当节更改(另一个div为“活动”)时,固定背景仍然具有第一个活动节背景色。

  • or use php with custom query outside a loop - but I don't know how to filter with a specific class name "active". 或在循环外使用带有自定义查询的php-但我不知道如何使用特定的类名“ active”进行过滤。 I have only basic script: 我只有基本的脚本:

     style="background-color:<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'background', true); wp_reset_query(); 

how to add a filter for "active" class? 如何为“活动”类添加过滤器?

Any other ideas? 还有其他想法吗? Help! 救命!

EDIT2 : I found two scripts which do what I need: EDIT2 :我发现了两个可以满足我需要的脚本:

1 https://codepen.io/Funsella/pen/yLfAG 1 https://codepen.io/Funsella/pen/yLfAG

2 https://codepen.io/Funsella/pen/dpRPYZ 2 https://codepen.io/Funsella/pen/dpRPYZ

both change the background-color copying it from "source" div to second "target" div. 两者都更改了背景色,将其从“源” div复制到第二个“目标” div。 I can keep "source" div inside a wordpress loop (fullpage wrapper too) and transfer the background-color value to my fixed background div outside of WP's loop and fullpage. 我可以将“ source” div保留在wordpress循环(也包括整页包装器)中,并将background-color值传输到WP的循环和整页之外的固定背景div中。 First script works with scroll events - so it probably won't work with fullpage.js plugin which doesn't scroll content. 第一个脚本适用于滚动事件-因此它可能不适用于不滚动内容的fullpage.js插件。 But the second script does his job exactly how I needed. 但是第二个脚本正是按照我的需要来完成他的工作。 I tested it. 我测试了

So the question is: can it be done in more simple way? 所以问题是:可以用更简单的方式完成吗? Without additional jQuery plugin (in-view.js)? 是否没有其他jQuery插件(in-view.js)? Just plain JS or jQuery? 只是普通的JS或jQuery?

So, you'll be showing various blog posts on a single page and want each one to have its own background color, correct? 因此,您将在单个页面上显示各种博客文章,并希望每个文章都有自己的背景颜色,对吗? If so, try fullpage.js' sectionsColor option. 如果是这样,请尝试使用fullpage.js的sectionsColor选项。

You can read about it here https://github.com/alvarotrigo/fullPage.js#options 您可以在这里阅读有关内容https://github.com/alvarotrigo/fullPage.js#options

Here's an example from that page: 这是该页面上的示例:

$('#fullpage').fullpage({
    sectionsColor: ['#f2f2f2', '#4BBFC3', '#7BAABE', 'whitesmoke', '#000'],
});

The documentation doesn't explain precisely what's happening here, but it looks like you're simply sending in an array of section colors in order of their appearance. 该文档没有确切解释此处发生的情况,但看起来您只是按颜色顺序发送了一系列颜色。

EDIT BASED ON COMMENTS: 根据评论进行编辑:

From your picture I think I better understand what you're trying to do. 从您的照片中,我想我会更好地了解您正在尝试做什么。 Basically you want a slide layer (transparent) and a fixed background layer (color of active slide). 基本上,您需要一个幻灯片图层(透明)和一个固定的背景图层(活动幻灯片的颜色)。 First, is this parallex example what you're trying to achieve? 首先,这个parallex示例是您要实现的目标吗? https://alvarotrigo.com/fullPage/extensions/parallax.html#firstPage https://alvarotrigo.com/fullPage/extensions/parallax.html#firstPage

If so, there is a basic example in the repo. 如果是这样,则回购中有一个基本示例。 If this won't accomplish exactly what you're trying to do, follow these directions: 如果这样做不能完全完成您要执行的操作,请按照以下说明进行操作:

  1. Use the onLeave or afterLoad callback to make an ajax request to your site. 使用onLeave或afterLoad回调向您的站点发出ajax请求。
  2. Query your posts using your post Id to retrieve the post attribute background. 使用帖子ID查询帖子以检索帖子属性背景。
  3. Retrieve the background color and set/fade-in your background-color property. 检索背景色并设置/淡入背景色属性。

Here's a very simple ajax request example using jQuery: 这是一个使用jQuery的非常简单的ajax请求示例:

//set the function to perform for onLeave in the options
onLeave: goGetThatColor(index)

//not in your options
function goGetThatColor(index){
    $.ajax({
      method: "POST",
      url: "get_post_color.php",
      data: { id: index }
    })
      .done(function( color ) {
        $(#background-element).css('background-color', color);
      });
}

I'm just going based off of the documentation, the index that's passed in might be an arbitrary ordering or the post id from Wordpress. 我只是基于文档,传递的索引可能是任意顺序或Wordpress的帖子ID。 I'm not sure about that so you might have to grab the post id from an attribute in your dom. 我不确定,因此您可能必须从dom属性中获取帖子ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM