简体   繁体   English

根据变量更改鼠标事件

[英]Changing mouse events depending on a variable

I bet this is really simple and im gonna try to explain it the best I can: I am trying to build a gallery to show different transitions and reveals ( making an overlayed div transition out of sight, for instance ), and I want there to be a button that the user can click to experience the same transition with hover and with clicks. 我敢打赌,这真的很简单,我将尽我所能进行解释:我正在尝试建立一个画廊以展示不同的过渡效果并进行展示(例如,使div过渡过渡到视线之外),我想在那里是一个按钮,用户可以单击该按钮以体验鼠标悬停和单击时的相同过渡。

To achieve this I simply created a new variable that I called toggleButton , initialized it at 0 , and made a conditional statement saying that should the variable be 0, the transition should be working on hover, and, should toggleButton be 1 , the transition would work with clicks. 为此,我只创建了一个名为toggleButton的新变量,将其初始化为0 ,并执行了一条条件语句,说该变量应为0,过渡应该在悬停上工作,并且toggleButton应该为1 ,过渡将使用点击。 I am logging the variable changes on the console everytime the button to change them is pressed, and it works fine, no errors, but I can't achieve what i wanted to, the transition never changes, it always stays the way i initialize it ( if i init it at 1 then the transition works with clicks ). 每次按下更改按钮的更改时,我都会在控制台上记录变量更改,它可以正常工作,没有错误,但是我无法实现我想要的功能,过渡永远不会改变,它始终保持初始化的方式(如果我在1初始化它,那么转换将随着clicks一起工作)。

I made a fiddle with a little example of what I'm talking about, I hope you can understand what I want by looking at it in case i failed at explaining it with words: http://jsfiddle.net/mcczrbgg/1/ 我在讲一个小例子来说明我的意思,希望您能通过查看它了解我想要的内容,以防万一我无法用单词解释它: http : //jsfiddle.net/mcczrbgg/1/

Thanks in advance! 提前致谢!

That If/Else that sets up mouseovers on line 21 only runs once. 在第21行设置鼠标悬停的If / Else仅运行一次。

Try making a .mouseenter() function AND the .click() function, but in each one, check the state of toggle button: 尝试制作.mouseenter()函数和.click()函数,但在每个函数中,检查切换按钮的状态:

 $(".thumb").mouseenter(function() {
      if (toggleButton === 0) {

      }
 }
 $(".thumb").click(function() {
      if (toggleButton === 1) {

      {
 }

Is that what you had in mind? 那是你的想法吗?

The reason it is not working is because youre only running the function once at load. 它不起作用的原因是因为您仅在加载时运行了该函数一次。 You need to also make sure the function runs every time you run the toggle event. 您还需要确保该函数在每次运行toggle事件时都运行。

This is what I would do: 这就是我要做的:

$(function(){

    var toggleButton = 0 ;
    console.log(toggleButton);

    $('.switchButton').toggle(function(){
        toggleButton = 1 ;
        $('.toggleButton').css({'transform' : 'rotateZ(0deg)'});
        $('#hover').removeClass('active');
        $('#click').addClass('active');
        console.log(toggleButton);
        runTransition();
        }, function(){
        toggleButton = 0 ;
        $('.toggleButton').css({'transform' : 'rotateZ(180deg)'});
        $('#hover').addClass('active');
        $('#click').removeClass('active');
        console.log(toggleButton);
        runTransition();
    });
    runTransition();
// thumb hover 
    function runTransition(){
        if ( toggleButton == 0 ) {
            $('.thumb').mouseenter(function(){
                $('.overlay').css({'left' : '-100%'});
            }).mouseleave(function(){
                $('.overlay').css({'left' : '0%'});
            });
        } else {
            $('.thumb').unbind('mouseenter mouseleave');

            $('.thumb').toggle(function(){
                $('.overlay').css({'left' : '-100%'});
            }, function(){
                $('.overlay').css({'left' : '0%'});
            });
        }
    }
});

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

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