范围输入的JavaScript位置问题

时间:2014-05-26 14:34:45

标签: javascript jquery html css

大家下午好。

我遇到了JavaScript / CSS / HTML问题,让我花点时间解释一下

  1. 我为我运行的网站编写了一个报价系统(http://www.liquidationmadeeasy.co.uk/quote.html

  2. 我遵循了一个使用框架集的示例,以便为引用创建不同的阶段,但不导航到单独的页面。这很棒。

  3. 在每个字段集上,我引入了一个范围输入。我将执行计算并强制用户输入数值。

  4. 然后我按照另一个教程介绍了一个显示范围控件选定值的气泡。

  5. 当页面加载时,它会在每个范围控件的侧面绘制气泡。然而,当我导航到第二个框架集时,所有项目的气泡都在页面中间绘制(不在正确的位置)并且全部在彼此之上。

  6. 当用户从范围控制中选择一个值时,气泡会在正确的位置重新绘制。

  7. 当用户离开第一阶段时,我想自动重绘报价第二阶段的所有气泡。

    我认为这是失败的,因为我重置了更改事件的范围输入值,该值仅在用户更改值时触发

    有人能指出我正确的方向

    HTML

        <form id="msform" method="post" action="quoteresult.php">
        <!-- progressbar -->
        <ul id="progressbar">
            <li class="active">Debts</li>
            <li>Assets</li>
            <li>Contact</li>
        </ul>
        <!-- fieldsets -->
        <fieldset>
            <h2 class="fs-title">What You Owe</h2>
            <h3 class="fs-subtitle">Your companys financial possition</h3>
            <label for="overdraft">Overdraft:</label>
            <input type="range" name="overdraft" id="overdraft" value=0 min=0 max=1000000 step="500" />
            <output for="overdraft">1</output>
            <label for="vat">VAT:</label>
            <input type="range" name="vat" id="vat" value=0 min=0 max=1000000 step="500" />
            <output for="vat">1</output>
            <label for="paye">PAYE:</label>
            <input type="range" name="paye" value=0 min=0 max=1000000 step="500"/>
            <output for="paye">1</output>
            <label for="corporationtax">Corporation Tax:</label>
            <input type="range" name="corporationtax" value=0 min=0 max=1000000 step="500" />
            <output for="corporationtax">1</output>
            <label for="tradecreditors">Trade Creditors:</label>
            <input type="range" name="tradecreditors" value=0 min=0 max=1000000 step="500"  />
             <output for="tradecreditors">1</output>
             <label for="rent">Rent:</label>
            <input type="range" name="rent" value=0 min=0 max=1000000 step="500" />
            <output for="rent">1</output>
            <input type="button" name="next" class="next action-button" value="Next" />
        </fieldset>
        <fieldset>
            <h2 class="fs-title">Asset Values</h2>
            <h3 class="fs-subtitle">What are you assets worth?</h3>
            <label for="debtors">Debtors:</label>
            <input type="range" name="debtors" value=0 min=0 max=1000000 step="500" />
            <output for="debtors">1</output>
            <label for="fixtures">Fixtures:</label>
            <input type="range" name="fixtures"  value=0 min=0 max=1000000 step="500"/>
            <output for="fixtures">1</output>
            <label for="stock">Stock:</label>
            <input type="range" name="stock" value=0 min=0 max=1000000 step="500" />
            <output for="stock">1</output>
            <label for="vehicles">Vehicles:</label>
            <input type="range" name="vehicles" value=0 min=0 max=1000000 step="500" />
            <output for="vehicles">1</output>
            <label for="cash">Cash:</label>
            <input type="range" name="cash" value=0 min=0 max=1000000 step="500" />
            <output for="cash">1</output>
            <label for="plant">Plant/Machinery:</label>
            <input type="range" name="plant" value=0 min=0 max=1000000 step="500"/>
            <output for="plant">1</output>
            <label for="property">Property:</label>
            <input type="range" name="property" value=0 min=0 max=1000000 step="500" />
            <output for="property">1</output>
            <label for="other">Other:</label>
            <input type="range" name="other" value=0 min=0 max=1000000 step="500" />
            <output for="other">1</output>
            <input type="button" name="previous" class="previous action-button" value="Previous" />
            <input type="button" name="next" class="next action-button" value="Next" />
        </fieldset>
        <fieldset>
            <h2 class="fs-title">Personal Details</h2>
            <h3 class="fs-subtitle">We will never sell it</h3>
            <input type="text" name="fname" placeholder="First Name" />
            <input type="text" name="lname" placeholder="Last Name" />
            <input type="text" name="company" placeholder="Company Name" />
            <input type="text" name="phone" placeholder="Phone" />
            <input type="text" name="email" placeholder="Email" />
            <input type="button" name="previous" class="previous action-button" value="Previous" />
            <input type="submit" name="submit" class="submit action-button" value="Submit" />
        </fieldset>
    </form>
    

    CSS

        /*form styles*/
        #msform {
        width: 400px;
        margin: 50px auto;
        text-align: center;
        position: relative;
    }
        #msform fieldset {
        background: white;
        border: 0 none;
        border-radius: 3px;
        box-shadow: 0 0 15px 1px rgba(0, 0, 0, 0.4);
        padding: 20px 30px;
    
        box-sizing: border-box;
        width: 80%;
        margin: 0 10%;
    
        /*stacking fieldsets above each other*/
        position: absolute;
    }
        /*Hide all except first fieldset*/
        #msform fieldset:not(:first-of-type) {
        display: none;
    }
        /*inputs*/
        #msform input, #msform textarea {
        padding: 15px;
        border: 1px solid #ccc;
        border-radius: 3px;
        margin-bottom: 10px;
        width: 100%;
        box-sizing: border-box;
        font-family: montserrat;
        color: #2C3E50;
        font-size: 13px;
    }
        /*buttons*/
        #msform .action-button {
        width: 100px;
        background: #27AE60;
        font-weight: bold;
        color: white;
        border: 0 none;
        border-radius: 1px;
        cursor: pointer;
        padding: 10px 5px;
        margin: 10px 5px;
        }
        #msform .action-button:hover, #msform .action-button:focus {
        box-shadow: 0 0 0 2px white, 0 0 0 3px #27AE60;
        }
        /*headings*/
        .fs-title {
        font-size: 15px;
        text-transform: uppercase;
        color: #2C3E50;
        margin-bottom: 10px;
        }
        .fs-subtitle {
        font-weight: normal;
        font-size: 13px;
        color: #666;
        margin-bottom: 20px;
        }
        /*progressbar*/
        #progressbar {
        margin-bottom: 30px;
        overflow: hidden;
        /*CSS counters to number the steps*/
        counter-reset: step;
        }
        #progressbar li {
        list-style-type: none;
        color: white;
        text-transform: uppercase;
        font-size: 9px;
        width: 33.33%;
        float: left;
        position: relative;
        }
        #progressbar li:before {
        content: counter(step);
        counter-increment: step;
        width: 20px;
        line-height: 20px;
        display: block;
        font-size: 10px;
        color: #333;
        background: white;
        border-radius: 3px;
        margin: 0 auto 5px auto;
    }
    /*progressbar connectors*/
    #progressbar li:after {
        content: '';
        width: 100%;
        height: 2px;
        background: white;
        position: absolute;
        left: -50%;
        top: 9px;
        z-index: -1; /*put it behind the numbers*/
    }
    #progressbar li:first-child:after {
        /*connector not needed before the first step*/
        content: none; 
    }
    /*marking active/completed steps green*/
    /*The number of the step and the connector before it = green*/
    #progressbar li.active:before,  #progressbar li.active:after{
        background: #27AE60;
        color: white;
    }
    

    的JavaScript

    //jQuery time
    var current_fs, next_fs, previous_fs; //fieldsets
    var left, opacity, scale; //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+')'});
                next_fs.css({'left': left, 'opacity': opacity});
            }, 
            duration: 800, 
            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});
                previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
                this.redraw();
            }, 
            duration: 800, 
            complete: function(){
                current_fs.hide();
                animating = false;
    
            }, 
            //this comes from the custom easing plugin
            easing: 'easeInOutBack'
        });
    });
    
    $(".submit").click(function(){
        document.getElementById("msform").submit();
    
        return false;
    })
    
         $(function() {
           var el, newPoint, newPlace, offset, newTop;
           $("input[type='range']").change(function() {
             el = $(this);
             width = el.width();
             newPoint = (el.val() - el.attr("min")) / (el.attr("max") - el.attr("min"));
             offset = -1.3;
             newTop = el.position().top - 10;
             newLeft = el.position().left + width + 10;
             if (newPoint < 0) { newPlace = 0;  }
             else if (newPoint > 1) { newPlace = width; }
             else { newPlace = width * newPoint + offset; offset -= newPoint;}
             el
               .next("output")
               .css({
                 left: newLeft + "px",
                 top: newTop + "px"
               })
               .text(el.val());
           })
           .trigger('change');
         });
    

1 个答案:

答案 0 :(得分:1)

这不是一个建议的解决方案,但我必须这样发布,所以我可以上传我的结果的截图。我逐字复制了你的HTML,CSS和JS(当然我必须适当地包装HTML),而且我没有遇到你描述的错误。 我尝试使用两个不同的jQuery CDN文件:

以下是Firefox 29.0.1,Chrome 35,Safari 6.1.4和IE10中呈现的第二阶段&#34;资产价值&#34;的屏幕截图。前三个是Mac OS X 10.7.5,后者是在VirtualBox /上运行的Windows 8 VM上。订单是Firefox,Chrome,Safari,IE:

Firefox 29.0.1

Chrome 35

Safari 6.1.4

IE10

正如您所看到的,除了Firefox之外,渲染在某种程度上在所有浏览器中都被破坏了,但是您描述的错误并未出现。 我还注意到您链接到的网站:http://www.liquidationmadeeasy.co.uk/quote.html ...在Firefox 29中似乎也运行良好:

Live site

您想发布用于测试的确切浏览器吗?


P.S。

与您的问题无关,但请注意,您的大多数HTML元素都有&#34;对于&#34;指向&#34; name&#34;的属性而不是&#34; id&#34;属性。请参阅<label> element in MDN documentation

以下标签就是这种情况:

第一个字段集:

    <label for="paye">PAYE:</label>
    <input type="range" name="paye" value=0 min=0 max=1000000 step="500"/>
    <output for="paye">1</output>
    <label for="corporationtax">Corporation Tax:</label>
    <input type="range" name="corporationtax" value=0 min=0 max=1000000 step="500" />
    <output for="corporationtax">1</output>
    <label for="tradecreditors">Trade Creditors:</label>
    <input type="range" name="tradecreditors" value=0 min=0 max=1000000 step="500"  />
    <output for="tradecreditors">1</output>
    <label for="rent">Rent:</label>
    <input type="range" name="rent" value=0 min=0 max=1000000 step="500" />

第二个字段集:

    <label for="debtors">Debtors:</label>
    <input type="range" name="debtors" value=0 min=0 max=1000000 step="500" />
    <output for="debtors">1</output>
    <label for="fixtures">Fixtures:</label>
    <input type="range" name="fixtures"  value=0 min=0 max=1000000 step="500"/>
    <output for="fixtures">1</output>
    <label for="stock">Stock:</label>
    <input type="range" name="stock" value=0 min=0 max=1000000 step="500" />
    <output for="stock">1</output>
    <label for="vehicles">Vehicles:</label>
    <input type="range" name="vehicles" value=0 min=0 max=1000000 step="500" />
    <output for="vehicles">1</output>
    <label for="cash">Cash:</label>
    <input type="range" name="cash" value=0 min=0 max=1000000 step="500" />
    <output for="cash">1</output>
    <label for="plant">Plant/Machinery:</label>
    <input type="range" name="plant" value=0 min=0 max=1000000 step="500"/>
    <output for="plant">1</output>
    <label for="property">Property:</label>
    <input type="range" name="property" value=0 min=0 max=1000000 step="500" />
    <output for="property">1</output>
    <label for="other">Other:</label>
    <input type="range" name="other" value=0 min=0 max=1000000 step="500" />