简体   繁体   English

将自定义.js文件作为背景添加到Wordpress

[英]Adding Custom .js file to Wordpress as a Background

I have been given the .js files to create this vignette effect. 我得到了.js文件来创建此小插图效果。

https://demo.projectwyvern.com/ https://demo.projectwyvern.com/

I cannot for the life of me get the file to work on my homepage. 我一生都无法在主页上使用该文件。 I am using the Theme Astra and the elementor page builder. 我正在使用Theme Astra和elementor页面构建器。

I would like the .js to work as a background and for elementor to call the js. 我希望.js用作背景,让elementor调用js。

I have tried many ways including adding the .js into the header, but all I got was a load of code on the page. 我尝试了很多方法,包括将.js添加到标题中,但是我所得到的只是页面上的大量代码。 I have been told by the creator 'to put a div element with id '#container' on the page.' 创建者已告诉我“在页面上放置ID为'#container'的div元素”。

I've also tried a few custom code plugins. 我还尝试了一些自定义代码插件。 I feel like I need to use the enqueue script in the functions.php? 我觉得我需要在functions.php中使用入队脚本吗?

Any help would be greatly appreciated. 任何帮助将不胜感激。

All the files given to be by the creator can be found on his github. 创建者提供的所有文件都可以在他的github上找到。

https://github.com/ProjectWyvern/wyvern-logo-effects https://github.com/ProjectWyvern/wyvern-logo-effects

You need to alter the header.php template for your theme (you should probably create a child theme and alter that). 您需要更改主题的header.php模板(您可能应该创建一个子主题并对其进行更改)。

Per the example at that github project: 根据该github项目的示例:

Add something like this to the header.php 在header.php中添加类似的内容

<div id="container"></div>
<img src="static/logo.svg" id="logo" style="display: none; height: 100%; width: 100%;" />

Then, make a javascript file with code like this, which is what activates your effect: 然后,使用以下代码制作一个javascript文件,这可以激活您的效果:

 var canvas = document.createElement('canvas'); canvas.id = 'canvas'; canvas.height = window.innerHeight; canvas.width = window.innerWidth; document.getElementById('container').appendChild(canvas); var seriously = new Seriously(); var logo = seriously.source('#logo'); var target = seriously.target('#canvas'); var vignette = seriously.effect('vignette'); vignette.amount = 1; vignette.source = logo; var mask = seriously.transform(); mask.source = vignette; target.source = mask; var factor = 1; var diff = 0; function setPosition(x, y) { x -= canvas.offsetLeft; y -= canvas.offsetTop; var xd = Math.abs(x - (canvas.width / 2)) / canvas.width; var yd = Math.abs(y - (canvas.height / 2)) / canvas.height; diff = xd + yd; } function mouseMove(e) { setPosition(e.pageX, e.pageY); } window.addEventListener('mousemove', mouseMove, false); var amt = 0.5; var del = 200; mouseMove({pageX: 0, pageY: 0}) seriously.go(function(now) { factor = amt + (Math.sin(now / del) / Math.PI / 8); vignette.amount = factor * 100 * diff; }); 

Next, save serious.js and your new javascript file into theme's scripts directory 接下来,将认真的.js和新的javascript文件保存到主题的脚本目录中

[astratheme-child]/scripts/seriously.js
[astratheme-child]/scripts/activateseriously.js

Then, in functions.php file, enqueue the two javascript files: 然后,在functions.php文件中,使两个javascript文件入队:

wp_enqueue_script('seriously', get_stylesheet_directory_uri() . '/scripts/seriously.js');
wp_enqueue_script('seriously', get_stylesheet_directory_uri() . '/scripts/activateseriously.js');

If you have trouble with it trying to run before the page loads all the way, remove the enqueue calls from functions.php, and instead add the two scripts as tags in the footer.php. 如果您在尝试在页面完全加载之前尝试运行时遇到麻烦,请从functions.php删除入队调用,然后将两个脚本作为标签添加到footer.php中。

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

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