动画div / fieldset中的多步形式

时间:2017-04-24 13:39:15

标签: javascript jquery html css

我已经创建了一个循序渐进的演练。每个步骤都包含在一个单独的"字段集"中,整个是一个.html文件。

要前进和后退我使用javascript。 问题是,随着时间的推移,动画开始滞后。

在附带的例子中,一切都很顺利,因为内容很少。 优化问题在哪里?

JsFiddle

HTML:

 <form id="msform">
             <fieldset id="firstField">   
                      <input type="button" name="subject1" class="next action-button" value="Subject1 />
<input type="button" name="subject2" class="next action-button" value="Subject2 />
<input type="button" name="subject3" class="next action-button" value="Subject3 />
             </fieldset>
             <fieldset id="subject1">
            <h2>Some text</h2>
            <input type="button" name="prev" class="back action-button" value="Back" />
            <input type="button" name="previous" class="previous action-button" value="Reset" />
         </fieldset>
    </form>

使用Javascript:

  var current_fs, next_fs, previous_fs, back_fs;
     var left, opacity, scale;
     var animating;
     var progress = [];
     var message = "Funkcja niedostępna.";
     $(".next").click(function() {
         if (animating) return false;
         animating = true;
         current_fs = $(this).parent();
         next_fs = $('#' + $(this).attr('name'));

         $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");
         progress.push($('fieldset:visible'));

         next_fs.show();
         current_fs.animate({
             opacity: 0
         }, {
             step: function(now, mx) {
                 scale = 1 - (1 - now) * 0.2;
                 left = (now * 50) + "%";
                 opacity = 1 - now;
                 next_fs.css({
                     'left': left,
                     'opacity': opacity
                 });
             },
             duration: 800,
             complete: function() {
                 current_fs.hide();
                 animating = false;
             },
             easing: 'easeInOutBack'
         });
     });
     $(".previous").click(function() {
         if (animating) return false;
         animating = true;
         current_fs = $(this).parent();
         previous_fs = $('#firstField');

         $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

         previous_fs.show();
         current_fs.animate({
             opacity: 0
         }, {
             step: function(now, mx) {
                 scale = 0.8 + (1 - now) * 0.2;
                 left = ((1 - now) * 50) + "%";
                 opacity = 1 - now;
                 current_fs.css({
                     'left': left
                 });
                 previous_fs.css({
                     'transform': 'scale(' + scale + ')',
                     'opacity': opacity
                 });
             },
             duration: 800,
             complete: function() {
                 current_fs.hide();
                 animating = false;
             },
             easing: 'easeInOutBack'
         });
     });
     $(".back").click(function() {
         if (animating) return false;
         animating = true;
         current_fs = $(this).parent();
         $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

         back_fs = progress.pop();
         back_fs.show();
         current_fs.animate({
             opacity: 0
         }, {
             step: function(now, mx) {
                 scale = 0.8 + (1 - now) * 0.2;
                 left = ((1 - now) * 50) + "%";
                 opacity = 1 - now;
                 current_fs.css({
                     'left': left
                 });
                 back_fs.css({
                     'transform': 'scale(' + scale + ')',
                     'opacity': opacity
                 });
             },
             duration: 800,
             complete: function() {
                 current_fs.hide();
                 animating = false;
             },
             easing: 'easeInOutBack'
         });
     });

1 个答案:

答案 0 :(得分:0)

您是否能够使按钮在div中工作?

我使用jQuery和CSS3&#34;下载了相同的&#34;带有进度条的多步表单。

我能够显示带有div的字段集但是实际的按钮......除非在div之外,否则onclick不起作用。

所以这一步进入下一步

  <fieldset>
<h2 class="fs-title">Create your account</h2>
<h3 class="fs-subtitle">This is step 1</h3>
<input type="text" name="email" placeholder="Email" />
<input type="password" name="pass" placeholder="Password" />
<input type="password" name="cpass" placeholder="Confirm Password" />
<input type="button" name="next" class="next action-button" value="Next" />

虽然这个没有

  <fieldset>
<h2 class="fs-title">Create your account</h2>
<h3 class="fs-subtitle">This is step 1</h3>
<input type="text" name="email" placeholder="Email" />
<input type="password" name="pass" placeholder="Password" />
<input type="password" name="cpass" placeholder="Confirm Password" />
<div class="pull-right">
    <input type="button" name="next" class="next action-button" value="Next" />                
</div>

我没有尝试过任何东西......即使删除动画也没有用

回答你的问题......:)

我改变了我的JS以禁用(注释掉)尽可能多的动画并使持续时间为000 ......

//jQuery time
var current_fs, next_fs, previous_fs; //fieldsets
var opacity; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

$(".next").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show(); 
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            //scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            //left = (now * 50)+"%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            //current_fs.css({'transform': 'scale('+scale+')'});
            current_fs.css({'opacity': opacity});
            //next_fs.css({'left': left, 'opacity': opacity});
            next_fs.css({'opacity': opacity});
        }, 
        duration: 000, 
        complete: function(){
            current_fs.hide();
            animating = false;
        }, 
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".previous").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    previous_fs = $(this).parent().prev();

    //de-activate current step on progressbar
    $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

    //show the previous fieldset
    previous_fs.show(); 
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale previous_fs from 80% to 100%
            //scale = 0.8 + (1 - now) * 0.2;
            //2. take current_fs to the right(50%) - from 0%
            //left = ((1-now) * 50)+"%";
            //3. increase opacity of previous_fs to 1 as it moves in
            opacity = 1 - now;
            //current_fs.css({'left': left});
            current_fs.css({'opacity': opacity});
            //previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
            previous_fs.css({'opacity': opacity});
        }, 
        duration: 000, 
        complete: function(){
            current_fs.hide();
            animating = false;
        }, 
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});