javascript - New to Wordpress, CSS, JS, etc. Having trouble adding custom JS - works in console with desired results but not on

I apologize for my ignorance, I'm still learning. Trying really hard to understand this all and make it work.I hav

I apologize for my ignorance, I'm still learning. Trying really hard to understand this all and make it work.

I have a page with an image in a container and I want it to expand when the user clicks on it with an overlay appearing behind it. When clicked again, I want it to go back to normal size and overlay disappear. I'm able to do it via console with this JS code

const images = document.querySelectorAll('.wp-block-uagb-image img.uag-image-4563'); const overlay = document.createElement('div'); overlay.classList.add('uagb-overlay'); document.body.appendChild(overlay); // Style the overlay overlay.style.position = 'fixed'; overlay.style.top = '0'; overlay.style.left = '0'; overlay.style.width = '100vw'; // Full viewport width overlay.style.height = '100vh'; // Full viewport height overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.7)'; overlay.style.display = 'none'; overlay.style.zIndex = '9999'; // High z-index to ensure it’s on top overlay.style.justifyContent = 'center'; overlay.style.alignItems = 'center'; images.forEach(image => { image.addEventListener('click', function() { // Create a new image element for the overlay const overlayImage = document.createElement('img'); overlayImage.src = image.src; // Same image source for the overlay overlayImage.style.maxWidth = '90%'; // Limit max size of the image overlayImage.style.maxHeight = '90%'; // Limit max size of the image overlayImage.style.objectFit = 'contain'; // Ensure the image fits within the overlay overlayImage.style.transition = 'transform 0.3s ease-in-out'; // Smooth scaling effect overlay.innerHTML = ''; // Clear any previous overlay content overlay.appendChild(overlayImage); // Add new image to overlay // Show overlay overlay.style.display = 'flex'; // Optional: Scale up the image for expansion effect overlayImage.style.transform = 'scale(1.2)'; }); }); // Close overlay when clicked overlay.addEventListener('click', function() { overlay.style.display = 'none'; // Hide overlay when clicked });

When trying to push it live, I tried this: Create custom-script.js in themes/astra-child/js folder Enqueue it with this in the themes/astra-child/functions.php

function custom_script_on_specific_page() {  if (is_page(2996)) { wp_enqueue_script('custom-script-js', get_stylesheet_directory_uri() . '/js/custom-script.js', [], false, true); } } add_action('wp_enqueue_scripts', 'custom_script_on_specific_page');

I also added this to themes/astra-child/style.css

img.uag-image-4563.expanded { transform: scale(1.5); transition: transform 0.3s ease; z-index: 1000; position: relative; } .uagb-overlay { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; background: rgba(0, 0, 0, 0.7); display: none; align-items: center; justify-content: center; z-index: 9999; } .uagb-overlay.visible { display: flex; }

and I added this to themes/astra-child/custom-script.js

document.addEventListener('DOMContentLoaded', function() { console.log("JavaScript is loaded and running!"); const images = document.querySelectorAll('.uag-image-4563'); if (images.length === 0) { console.log("No images with the specified class found."); } else { console.log("Images found:", images); } const overlay = document.createElement('div'); overlay.classList.add('uagb-overlay'); document.body.appendChild(overlay); overlay.style.position = 'fixed'; overlay.style.top = '0'; overlay.style.left = '0'; overlay.style.width = '100vw'; overlay.style.height = '100vh'; overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.7)'; overlay.style.display = 'none'; overlay.style.zIndex = '9999'; overlay.style.justifyContent = 'center'; overlay.style.alignItems = 'center'; images.forEach(image => { image.classList.remove('expanded'); image.addEventListener('click', function() { console.log("Image clicked!"); console.log("Clicked image:", this); this.classList.toggle('expanded'); console.log("Current class list:", this.classList); const overlayImage = document.createElement('img'); overlayImage.src = this.src; overlayImage.style.maxWidth = '90%'; overlayImage.style.maxHeight = '90%'; overlayImage.style.objectFit = 'contain'; overlayImage.style.transition = 'transform 0.3s ease-in-out'; overlay.innerHTML = ''; overlay.appendChild(overlayImage); overlay.style.display = 'flex'; overlayImage.style.transform = 'scale(1.2)'; console.log('Overlay visible:', overlay.style.display); }); }); overlay.addEventListener('click', function() { console.log("Overlay clicked. Hiding it."); overlay.style.display = 'none'; }); });

After saving everything, I purged cache via litespeed and cleared cache on hostinger

Any ideas why it works in console but not when I push it live? When I click the image on the live version, nothing happens but you can see the scrollbar jump like the image expanded.

I was troubleshooting with ChatGPT and Dom readiness was a hang up consistently. Troubleshooting would always halt there with codes being undefined in console

Thank you for any help

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744337246a4569190.html

相关推荐

  • Windows下的python安装教程_2024年10月最新最详细的安装指南

    文章目录 前言一、下载python二、安装python三、验证环境四、配置环境变量(可选)总结 前言 Python 是一种广泛使用的高级编程语言,以其简洁易读的语法和强大的

    1小时前
    00
  • 方法分享

    作者,Evil Genius现在的日子,关税在涨,物价在涨,钱是越来越难挣了。什么东西在跌呢?工资在跌,房价也跌,房租反而涨了,生活的压力感觉越来越大了。对于生信分析而言,什么东西是最重要的?尤其是空间转录组的分析?很显然,硬件是服务器,软

    1小时前
    00
  • win 7系统怎么看计算机配置,windows7系统怎么查看电脑配置

    通常我们在选购电脑的时候都比较注重电脑配置,那么windows7系统怎么查看电脑配置呢?接下来大家跟着学习啦小编一起来了解一下windows7查看电脑配置的解决方法吧。 windows7查看电脑配置方法一&#x

    1小时前
    00
  • 《Google Gemini 1.5 Pro:MoE架构如何重塑AI性能与效率》

    Google Gemini 1.5 Pro的诞生犹如一颗璀璨新星,吸引了无数目光。尤其是其采用的混合专家系统(MoE)架构扩展,为模型性能与推理效率带来了革命性的提升,在AI领域掀起了一阵讨论热潮。传统的人工智能模型就像是一个全能型选手,无

    59分钟前
    00
  • Power BI Vega 条形纵向折线组合图表

    条形图和纵向折线图组合常用来展示绝对值和率值的组合。之前在知识星球分享过SVG的版本(使用内置表格加载),今天换一种实现方式,使用Deneb视觉对象:把需要展示的维度和指标拖拽到Deneb,本例维度为城市,绝对值为[kpi]度量值,率值为[

    56分钟前
    00
  • 你的服务器够快吗?一文说清如何衡量服务器的性能?

    本文以一个标准的 Seurat 单细胞分析流程为例,深入探讨衡量计算性能的关键指标——运行时间(Elapsed Time)、CPU 时间(CPU Time)和等待时间(Wait Time)——揭示它们在单线程与多线程环境下的差异及其反映的系

    53分钟前
    00
  • windows11 x64 操作系统不支持Xilinx ISE 14.4版本,解决方案:创建windows7 x64 虚拟机,再安装Xilinx ISE 14.4。

    点击 下面的xsetup.exe可执行文件,弹出下面的报错框 : 于是我通过网上阅读文章进行了一系列的尝试 。 省流:1-7步骤都可以不用看,这些都是

    51分钟前
    00
  • 基于PacBio HiFi数据的人类全基因组重测序变异分析流程

    随着第三代测序技术,特别是PacBio HiFi(High Fidelity)测序技术的发展,我们能够获得兼具长读长和高准确度的测序数据。这为人类全基因组重测序(WGS)分析,尤其是复杂区域和结构性变异(Structural Variati

    49分钟前
    00
  • 扣子空间能战否?我看行~

    最近扣子推出了新模块:扣子空间,功能和前段时间刷屏的Manus类似,就是帮你一步到位完成任务。扣子空间地址:扣子空间现在还没完全开放,需要有邀请码,没邀请码的同学先加入等候名单,然后静静等待~这个扣子空间和普通的AI聊天工具有什么不一样呢?

    45分钟前
    00
  • 铜缆以太网19

    40G-CR4100G-CR10 (C85)PCS (C82)(二)发送方向发送过程发送过程基于从XLGMIICGMII接收到的TXD<63:0>和TXC<7:0>信号生成块。1个XLGMIICGMII数据发送

    42分钟前
    00
  • 皮尔兹Pnoz c1c2安全继电器配置与接线

    PNOZ compact是皮尔兹的一款经济型的安全继电器系列,价格实惠,空间紧凑,适用于安装急停按钮、安全门或光障光栅 等安全产品。PNOZ C1是适用急停按钮或安全门开关。PNOZ C2用于 4 类光障或带 OSSD 输出的传感器的安全

    40分钟前
    00
  • 计算机无法查看图片,Win7系统提示&quot;windows照片查看器无法打开此图片&quot;的解决方法...

    Win7系统经常会用Windows照片查看器查看图片&#xff0c;但是有位用户查看图片的时候出现提示“windows照片查看器无法打开此图片”,为什么会出现这样的问题呢&#xff1f;这是因为此文件可能已损坏、损毁或过大。

    39分钟前
    00
  • CMeas度量体系建设:让数据驱动更科学

    本文来自腾讯蓝鲸智云社区用户: CanWay研发效能度量在企业数字化转型中至关重要。它有助于企业全面量化研发过程,洞察业务绩效,识别效能瓶颈或提效机会、优化资源配置,并为决策提供有力支撑,持续推动企业研发效能优化提升。目前,效能度量已不局限

    37分钟前
    00
  • Sentinel 的熔断和限流

    在分布式系统里,服务之间牵一发而动全身,一个接口雪崩,可能带崩整个应用链路。要想系统抗住流量洪峰,顶住突发异常,就得在稳定性上下功夫。今天我就来说说稳定性保障里的老将——Sentinel,看看它是怎么凭借限流熔断,在服务治理的江湖里占得一席

    34分钟前
    00
  • 得物增长兑换商城的构架演进

    一、简介在移动互联网蓬勃发展的今天,用户的选择日益多元化,App市场的竞争也愈发白热化。为了在激烈的市场竞争中脱颖而出,提升用户获取效率并增强用户粘性,越来越多的应用开始采用积分兑换、抽奖等互动玩法。这些精心设计的运营策略不仅能够满足用户的

    22分钟前
    20
  • goframe这个劲爆的脚手架做项目还是非常不错

    goframe一个由国人封装的go语言框架,模块化的设计理念非常不错。要学习goframe框架内的相关技术,找一个成熟的系统进行研读是入门提高的不错选择。今天分享一个基于goframe框架的开发的后台管理系统。功能非常不错,而且前后端代码都

    20分钟前
    00
  • MODIS遥感影像批量下载方法

      本文介绍在Earthdata中批量下载MODIS遥感影像各产品数据的方法。  前期我们介绍了批量下载Landsat与MODIS等遥感影像的最新可行方法,本文则再介绍一种基于Earthdata的MODIS批量下载方法。但要注意,此方法有的

    17分钟前
    20
  • OFC 2025:IBM开发应用于CPO的高密度聚合物波导光子模块

    一、技术背景:突破电互连瓶颈在服务器硬件中,计算单元的带宽每两年以约 3 倍的速度增长,而互连技术仅以 1.4 倍的速度发展,导致 “带宽鸿沟” 持续扩大。传统可插拔收发模块虽能扩展传输距离,但在高密度集成和功耗上存在局限。

    10分钟前
    00
  • Nature证实:ChatGPT能够有效辅助医生提升决策准确率

    近期,Nature Medicine 刊登了一篇关于GPT-4在临床应用的文章《GPT-4 assistance for improvement of physician performance on patient care tasks:

    2分钟前
    00
  • IP 地址和网关是啥关系?看完你能自己配网络了

    IP 地址和网关是啥关系?看完你能自己配网络了前言肝文不易,点个免费的赞和关注,有错误的地方请指出,看个人主页有惊喜。作者:神的孩子都在歌唱你有没有遇到过这样的场景:“我家电脑连不上网,IP地址也有了,咋还是上不了网?”“这什么子网掩码

    34秒前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信