Safari浏览器中On-Off开关Checkbox动画卡顿问题求助
Fixing the Jumpy Toggle Switch in Safari
Hey there! Let's get that toggle switch sliding smoothly in Safari. The issue here is that Safari struggles with transitions on left/right properties when they jump between large values (like 5px to 85px). Instead, we can use transform: translateX() for the movement—this uses GPU acceleration and works consistently across all browsers, including Safari.
Here's the revised code with smooth Safari support:
<!DOCTYPE html> <html> <head> <style> .ra-box > input[type=checkbox] { opacity: 0; position: absolute; /* Ensure checkbox covers the entire toggle for reliable clicks */ width: 100%; height: 100%; margin: 0; cursor: pointer; } .ra-box { display: inline-block; margin: 0; padding: 6px; cursor: pointer; border-radius: 15px; overflow: hidden; background: #2a3542; width: 70px; height: 18px; position: relative; } /* ON button - fixed initial position */ .ra-box:before { height: 20px; width: 20px; background: #41cac0; color: #41cac0; content: 'ON'; text-indent: -40px; position: absolute; top: 5px; right: 5px; border-radius: 50%; transition: transform 0.25s ease-in-out, background-color 0.25s ease-in-out; } /* OFF button - fixed initial position */ .ra-box:after { height: 20px; width: 20px; background: #7f8c9a; color: #fff; content: 'OFF'; position: absolute; border-radius: 50%; text-indent: 30px; top: 5px; left: 5px; transition: transform 0.25s ease-in-out, background-color 0.25s ease-in-out; } /* Checked state - use transform to slide buttons smoothly */ .ra-box.checked:before { transform: translateX(-60px); /* Slide left by track width minus button size */ } .ra-box.checked:after { transform: translateX(60px); /* Slide right by matching amount */ } .ra-box.checked { background: #bdc3c7; transition: background-color 0.25s ease-in-out; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <div class="ra-box"> <input type="checkbox" id="toggle-1"> </div> <br /> <br /> <div class="ra-box"> <input type="checkbox" id="toggle-2" checked="checked" /> </div> <br /> <br /> <button type="button" id="test">Test</button> <script> $(document).ready(function () { // Handle toggle clicks without double-triggering $('.ra-box').click(function (e) { if (e.target.type !== 'checkbox') { $(this).toggleClass('checked'); const checkbox = $(this).find('input:checkbox'); checkbox.prop('checked', !checkbox.is(':checked')); } }); // Set initial toggle state based on checkbox $('.ra-box > input:checkbox').each(function () { $(this).parent().toggleClass('checked', $(this).is(':checked')); }); $('#test').on('click', function () { const a = $('#toggle-1').is(':checked') ? 1 : 0; const b = $('#toggle-2').is(':checked') ? 1 : 0; alert(a + ' ' + b); }); }); </script> </body> </html>
Key fixes explained:
- Switched to
transform: translateX(): Instead of adjustingleft/rightvalues (which Safari handles poorly for large jumps), we use transform to slide the buttons. This is GPU-accelerated and ensures smooth transitions everywhere. - Improved checkbox interaction: The checkbox now covers the entire toggle box, so clicks anywhere work reliably, and we avoid double-triggering the toggle state.
- Targeted transitions: We only animate properties that actually change (
transformandbackground-color), making the animation more efficient and consistent. - Cleaner initial state setup: Used
toggleClass('checked', condition)to neatly set the correct initial appearance based on the checkbox's checked status.
This will give you a buttery-smooth toggle that works flawlessly in Safari, Chrome, Firefox, and all modern browsers.
内容的提问来源于stack exchange,提问作者user2301515
相关产品推荐
相关产品推荐

