简体   繁体   English

Javascript 将“当前”选项卡镜像到另一个选项卡 | 网络流

[英]Javascript Mirror 'Current' Tab to another Tab | Webflow

I've created a widget in Webflow that works but the UX design isn't the greatest and in order for me to create a better experience I need help with some js.我在 Webflow 中创建了一个可以工作的小部件,但 UX 设计并不是最好的,为了让我创造更好的体验,我需要一些 js 的帮助。 Here's a little overview of the widget so you see what I mean:这是小部件的一个小概述,所以你明白我的意思:

The goal of the widget is to allow users to a) browse our product categories and continue to view product options b) view the aesthetic differences between our offered building types (prefab and container) while also allowing the user to compare building types for each category by following the "compare" CTA小部件的目标是允许用户 a) 浏览我们的产品类别并继续查看产品选项 b) 查看我们提供的建筑类型(预制件和容器)之间的美学差异,同时还允许用户比较每个类别的建筑类型按照“比较”CTA

Gif of Widget小部件的 Gif

We have 5 product categories in 2 different building types: prefab and container.我们在 2 种不同的建筑类型中有 5 种产品类别:预制件和集装箱。 I have 2 tabs to switch between the building types and then inside of those tabs content, I have 5 category tabs: Living space, portable offices, utility, recreation, and storage.我有 2 个选项卡可以在建筑类型之间切换,然后在这些选项卡内容中,我有 5 个类别选项卡:生活空间、便携式办公室、实用程序、娱乐和存储。 Those tabs' contents are the image and CTAs on the left.这些选项卡的内容是左侧的图像和 CTA。

My problem is the user's selected product category doesn't mirror over to the other building type.我的问题是用户选择的产品类别没有反映到其他建筑类型。 For example if you've selected office spaces on the prefab tab and switch over to the container tab, a completely different product category will be selected.例如,如果您在预制选项卡上选择了办公空间并切换到容器选项卡,则会选择完全不同的产品类别。

How it Should work它应该如何工作

I was able to mirror the selected prefab product category over to the container tab using this basic logic:我能够使用以下基本逻辑将选定的预制产品类别镜像到容器选项卡:

 <script> //prefab $(document).ready(function() { $('.livingprefab').click(function() { $(this).addClass('current'); $('.livingcontainer').trigger('tap'); }); $('.storageprefab').click(function() { $(this).addClass('current'); $('.storagecontainer').trigger('tap'); }); $('.utilityprefab').click(function() { $(this).addClass('current'); $('.utilitycontainer').trigger('tap'); }); $('.officeprefab').click(function() { $(this).addClass('current'); $('.officecontainer').trigger('tap'); }); $('.recprefab').click(function() { $(this).addClass('current'); $('.reccontainer').trigger('tap'); }); }); </script>

Now, this works but when I try to add the opposite (selected products on the container tab mirror to the prefab side) everything breaks.现在,这可行,但是当我尝试将相反的(容器选项卡镜像上的选定产品添加到预制侧)时,一切都会中断。 Why is this?为什么是这样? How can I fix it?我该如何解决?

PS This is literally my second time touching js so the code is probably far from best practice. PS 这实际上是我第二次接触 js,所以代码可能远非最佳实践。 I'm a UX designer trying to branch out into development so if you have any advice I'd love to read it.我是一名 UX 设计师,试图涉足开发领域,所以如果您有任何建议,我很乐意阅读。

Edit: Here's my Webflow read-only link编辑: 这是我的 Webflow 只读链接

Your code above makes an automatic click/tap occur on a container product just after a click occurs on the corresponding prefab product using the trigger method.上面的代码使用trigger方法在相应的预制产品上发生点击后立即在容器产品上发生自动点击/点击。

If you replicate this code in reverse as you suggest, an automatic click/tap will also occur on a prefab product just after a click on the corresponding container product.如果您按照您的建议反向复制此代码,则在单击相应的容器产品后,预制产品上也会发生自动单击/点击。 So— together these would create a never-ending recursive loop of clicks and I would imagine that everything would break.所以——这些一起会创建一个永无止境的点击递归循环,我想一切都会中断。

Instead of a click on one product triggering an automatic click/tap on the corresponding product, I suggest you use the click/tap events on a pair of products only to make changes directly to the classes on the relevant elements.我建议您使用一对产品上的点击/点击事件直接更改相关元素上的类,而不是点击一个产品触发相应产品的自动点击/点击。 I would avoid triggering additional click/tap events and in doing so you will avoid such recursive problems.我会避免触发额外的点击/点击事件,这样做可以避免此类递归问题。 And, as it looks like you are toggling the same class on the pair of corresponding products, you can apply the same function to both to avoid duplicating code.而且,看起来您在相应产品对上切换相同的 class,您可以对两者应用相同的 function 以避免重复代码。

It's hard to be precise without seeing the rest of your code and the HTML, but something like:如果没有看到代码的 rest 和 HTML,就很难准确,但类似:

function makeLivingCurrent() {
    $('.livingprefab').addClass('current');
    $('.livingcurrent').addClass('current');
}
$('.livingprefab, .livingcurrent').click( makeLivingCurrent ) 

And so on.等等。

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

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