admin管理员组文章数量:1296854
I am trying to enqueue a hover function in my functios.php of my child theme, so that when hovering over a title, a div with a text will be visible. The code I am using is this:
<?php
add_action( 'wp_enqueue_scripts', 'home_game', 20);
function home_game(){
?>
<script>
let title1 = document.getElementById("home-title1");
title1.addEventListener('mouseover', mouseOver);
title1.addEventListener('mouseout', mouseOut);
function mouseOver(){
document.getElementById('title-box').style.display = 'none';
document.getElementById('home-box1').style.display = 'block';
}
function mouseOut(){
document.getElementById('title-box').style.display = 'block';
document.getElementById('home-box1').style.display = 'none';
}
</script>
<?php
}
It works in my fiddle so I guess the problem is in my php, I am fairly new with it... Any help or clue will be very appreciated!
fiddle link: /
I am trying to enqueue a hover function in my functios.php of my child theme, so that when hovering over a title, a div with a text will be visible. The code I am using is this:
<?php
add_action( 'wp_enqueue_scripts', 'home_game', 20);
function home_game(){
?>
<script>
let title1 = document.getElementById("home-title1");
title1.addEventListener('mouseover', mouseOver);
title1.addEventListener('mouseout', mouseOut);
function mouseOver(){
document.getElementById('title-box').style.display = 'none';
document.getElementById('home-box1').style.display = 'block';
}
function mouseOut(){
document.getElementById('title-box').style.display = 'block';
document.getElementById('home-box1').style.display = 'none';
}
</script>
<?php
}
It works in my fiddle so I guess the problem is in my php, I am fairly new with it... Any help or clue will be very appreciated!
fiddle link: https://jsfiddle/rvoLc3ph/
Share Improve this question asked Mar 26, 2021 at 16:58 Guillermo BustilloGuillermo Bustillo 232 bronze badges2 Answers
Reset to default 0You can add your JS to a file in your child theme /js/example.js
, then enqueue it like this:
//add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );
function wpdocs_theme_name_scripts() {
wp_enqueue_script( 'script-name', get_stylesheet_directory_uri() . '/js/example.js', [], '1.0.0', true );
}
Alternatively, you can add the styles inline using this:
add_action( 'wp_enqueue_scripts', 'wpse_home_game', 20 );
function wpse_home_game() {
wp_register_script( 'home-game', false );
wp_enqueue_script( 'home-game' );
$script = "
let title1 = document.getElementById('home-title1');
// Check if title1 was present before wiring up event listeners.
if ( null !== title1 ) {
title1.addEventListener('mouseover', mouseOver);
title1.addEventListener('mouseout', mouseOut);
}
function mouseOver(){
document.getElementById('title-box').style.display = 'none';
document.getElementById('home-box1').style.display = 'block';
}
function mouseOut(){
document.getElementById('title-box').style.display = 'block';
document.getElementById('home-box1').style.display = 'none';
}
";
wp_add_inline_script( 'home-game', $script );
}
See for source of this trick from @birgire wp_add_inline_script without dependency
While you can do this with JS, remember that it's sketchy on mobile phones and doesn't always work. You might consider instead using a CSS 'tooltips' type of approach, which is pretty much just straight CSS with a hidden DIV that shows on hover.
You can play around with the generators, one of which is here:
https://doodlenerd/css-element/tooltip-generator
If you post more about how you're creating the title and the text within the DIV I may be able to suggest easy ways of crafting them on the fly too.....
本文标签: phpenqueue hover function
版权声明:本文标题:php - enqueue hover function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741646160a2390194.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论