启用/禁用备用样式表的 javascript

发布于 2024-12-25 15:06:22 字数 2372 浏览 4 评论 0原文

我正在处理的页面有两个样式表。当选择替代样式表时,我需要运行一个额外的函数来获得我想要的效果。所以我的问题是,如何编写 JavaScript 以仅在备用样式表处于活动状态时执行此代码块?

css 通过两个按钮和 styleswitcher.js 进行切换,如下所示:

<ul class="viewbuttons">
<li><a class="threedview" href="#" onclick="setActiveStyleSheet('default'); return false;">3D View</a></li>
<li><a class="twodview" href="#" onclick="setActiveStyleSheet('alternate'); return false;">2D View</a></li>
</ul>

就 javascript 而言,我采用了备用样式表特定代码 (twodview) 并将其放置在单击功能中,以便在样式表打开时激活切换,

$("a.twodview").click(function(){
all my terrible noob code
});

所以这显然是第一次切换到 2dview 时有效,但切换回来后代码仍然存在。我目前正在寻找的是如何沿着这些方向设置函数:

$("a.threedview").click(function(){
remove all my terrible noob code activated by the twodview
});

我探索的第一个选项是尝试将活动样式表定义为 while 循环中的变量,但令我惊讶的是,头部并没有真正改变使用 styleswitcher.js 所以我不明白它是如何切换 css (我认为很神奇)

这让我回到尝试使用 .click() 事件,如上面所示,有什么建议吗?

谢谢大家。

更新:

$("a.twodview").click( function() {
    $('img.small01').off('mouseenter');
    $('div#makeMeScrollable01').off('mouseleave')
    $('img.small02').off('mouseenter');
    $('div#makeMeScrollable02').off('mouseleave')
    $('img.small03').off('mouseenter');
    $('div#makeMeScrollable03').off('mouseleave')
    });
$("a.threedview").click( function() {
    $('img.small01').on('mouseenter');
    $('div#makeMeScrollable01').on('mouseleave')
    $('img.small02').on('mouseenter');
    $('div#makeMeScrollable02').on('mouseleave')
    $('img.small03').on('mouseenter');
    $('div#makeMeScrollable03').on('mouseleave')
    $('div#booma').off('click');
    });
$("a.twodview").click( function() {
    $('div#booma').click( function() {
    setTimeout(function(){$("div#makeMeScrollable01").addClass("currentslide");},2100);
    setTimeout(function(){$("div#makeMeScrollable01").appendTo("body");},2100);
    $("div#makeMeScrollable01").smoothDivScroll("enable");
    setTimeout(function(){$("div#makeMeScrollable01").smoothDivScroll();},2100);
    setTimeout(function(){$("img.small01").hide();},2100);
    });
});

所以我已经弄清楚如何在 3D 视图和 2D 视图之间切换悬停/单击功能,问题是 .on() 部分在切换回 3D 视图时应该重新绑定 hideIntent()不起作用。

也许问题出在hoverIntent()?将 .off() 分成 mouseenter 和 mouseleave 效果很好,所以我不确定情况是否如此。

我觉得我的 .on() 方法是正确的,但没有正确实现,有什么建议吗?

I have two style sheets for the page I'm working on. When the alternate style sheet is selected i need to run an extra function to get the effect I desire. So my question is, how can I write the javascript to only execute this block of code when the alternate style sheet is active?

The css is switched via two buttons and styleswitcher.js like so:

<ul class="viewbuttons">
<li><a class="threedview" href="#" onclick="setActiveStyleSheet('default'); return false;">3D View</a></li>
<li><a class="twodview" href="#" onclick="setActiveStyleSheet('alternate'); return false;">2D View</a></li>
</ul>

and as far as the javascript goes, i've taken the alternate style sheet specific code (twodview) and placed it within a click function to be activated when the style sheet is switched

$("a.twodview").click(function(){
all my terrible noob code
});

so this obviously works for the first time it is switched to the 2dview, but upon switching back the code remains. What I am currently looking for is how to set a function along these lines:

$("a.threedview").click(function(){
remove all my terrible noob code activated by the twodview
});

the first option I explored was trying to define the active stylesheet as a variable in a while loop, but much to my astonishment the in the head don't really change using styleswitcher.js so I don't understand how it is switching the css (magic i presume)

This brings me back to trying to use .click() events as seen above, any suggestions?

Thanks all.

update:

$("a.twodview").click( function() {
    $('img.small01').off('mouseenter');
    $('div#makeMeScrollable01').off('mouseleave')
    $('img.small02').off('mouseenter');
    $('div#makeMeScrollable02').off('mouseleave')
    $('img.small03').off('mouseenter');
    $('div#makeMeScrollable03').off('mouseleave')
    });
$("a.threedview").click( function() {
    $('img.small01').on('mouseenter');
    $('div#makeMeScrollable01').on('mouseleave')
    $('img.small02').on('mouseenter');
    $('div#makeMeScrollable02').on('mouseleave')
    $('img.small03').on('mouseenter');
    $('div#makeMeScrollable03').on('mouseleave')
    $('div#booma').off('click');
    });
$("a.twodview").click( function() {
    $('div#booma').click( function() {
    setTimeout(function(){$("div#makeMeScrollable01").addClass("currentslide");},2100);
    setTimeout(function(){$("div#makeMeScrollable01").appendTo("body");},2100);
    $("div#makeMeScrollable01").smoothDivScroll("enable");
    setTimeout(function(){$("div#makeMeScrollable01").smoothDivScroll();},2100);
    setTimeout(function(){$("img.small01").hide();},2100);
    });
});

So i've figured out how to kind of toggle the hover/click functionality between the 3D view and the 2D view, the problem is that the .on() portion that should rebind hoverIntent() when switched back to the 3D view doesn't work.

Maybe the problem is in hoverIntent()? separating the .off() into mouseenter and mouseleave works fine though so I'm not sure that is the case.

I feel as if my .on() is the right method but not implemented correctly, any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文