2014-02-08 179 views
0

我想在rails視圖中實現一個css動畫切換開關,但沒有成功。 我希望當開關被觸發後發佈一個動作到我的軌道控制器。Rails動畫切換開關

這裏是我的代碼: 在我看來:

<div class="onoffswitch"> 
<%= check_box 'onoffswitch', '0', id:"myonoffswitch", class: 'onoffswitch-checkbox'%> 
    <label class="onoffswitch-label" for="myonoffswitch"> 
     <div class="onoffswitch-inner"></div> 
     <div class="onoffswitch-switch"></div> 
    </label> 
</div> 

當開關被觸發我想要它做的這個帖子:

<%= link_to "switch",{controller: "static_pages", action: "local_switch"}, method: :post %> 

,然後我已根據「的CSS http://proto.io/freebies/onoff/':

.onoffswitch { 
    position: relative; width: 50px; 
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none; 
} 

.onoffswitch-checkbox { 
    display: none; 
} 

.onoffswitch-label { 
    display: block; overflow: hidden; cursor: pointer; 
    border: 50% solid #CCCCCC; border-radius: 22px; 
} 

.onoffswitch-inner { 
    width: 200%; margin-left: -100%; 
    -moz-transition: margin 0.2s ease-in 0s; -webkit-transition: margin 0.2s ease-in 0s; 
    -o-transition: margin 0.2s ease-in 0s; transition: margin 0.2s ease-in 0s; 
} 

.onoffswitch-inner:before, .onoffswitch-inner:after { 
    float: left; width: 50%; height: 5px; padding: 0; line-height: 5px; 
    font-size: 16px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold; 
    -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; 
} 

.onoffswitch-inner:before { 
    content: ""; 
    padding-left: 10px; 
    background-color: #3498DB; color: #FFFFFF; 
} 

.onoffswitch-inner:after { 
    content: ""; 
    padding-right: 10px; 
    background-color: #CCCCCC; color: #FFFFFF; 
    text-align: right; 
} 

.onoffswitch-switch { 
    width: 20px; margin: -7.5px; 
    background: #FFFFFF; 
    border: 2px solid #CCCCCC; border-radius: 22px; 
    position: absolute; top: 0; bottom: 0; right: 43px; 
    -moz-transition: all 0.2s ease-in 0s; -webkit-transition: all 0.2s ease-in 0s; 
    -o-transition: all 0.2s ease-in 0s; transition: all 0.2s ease-in 0s; 
} 

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner { 
    margin-left: 0; 
} 

.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch { 
    right: 0px; 
} 

謝謝!

+0

什麼是你的實際問題,這個CSS不工作發出,?或任何其他問題..? –

+0

我希望複選框在觸發時向我的控制器發佈操作 –

+0

您可以使用ajax –

回答

1

試試這個Ajax代碼...

$("#myonoffswitch").change(function(){ 
     if ($(this).is(":checked")) 
     { 
      $.ajax({ 
      type : "POST", 
      url  : "/static_pages/local_switch", # add your correct url 
      dataType: 'script', 
      data : { id:2 }, #u can pass any no of data 
      success : function(data){ 
          } 
      }); 
     } 
     else{ 

     } 
}); 

這個Ajax請求會當你的複選框被選中或切換到ON

+0

好吧,我會嘗試您的解決方案。謝謝你拉赫爾 –

+0

肯定會工作,這裏是jsfiddle.net/rahulsingh/gLp3T/,你只需要設置你的正確的Ajax請求 –