vanilla-tilt.js失效问题求助:卡片无法实现倾斜效果(4小时内急需协助)
Let’s walk through the most common fixes to get your tilt effect up and running quickly:
1. Explicitly Initialize Vanilla-Tilt
Sometimes relying solely on the data-tilt attribute isn’t enough—especially if your script version doesn’t auto-detect it. Add this initialization code after your vanilla-tilt.js import to manually trigger the effect:
<script type="text/javascript"> // Wait for the DOM to fully load before initializing document.addEventListener('DOMContentLoaded', function() { VanillaTilt.init(document.querySelector('.about-indiamain'), { max: 25, // Adjust maximum tilt angle (default is 20) speed: 400, // Adjust tilt animation speed glare: true // Optional: adds a glare effect for extra polish }); }); </script>
2. Check for CSS Conflicts
Your current styles are mostly fine, but add these properties to your .about-indiamain class to ensure the tilt effect can render properly:
.about-indiamain{ margin-left: 30%; margin-top: 10px; width: fit-content; font-size: 32px; background-color: yellowgreen; /* Critical for 3D tilt rendering */ transform-style: preserve-3d; perspective: 1000px; /* Optional: gives users a visual cue the card is interactive */ cursor: grab; }
Avoid adding custom transform properties to this element—vanilla-tilt uses transform to apply the tilt, and your own transforms will overwrite it.
3. Verify Script Path & Loading
Double-check that your vanilla-tilt.js file is in the correct location relative to your HTML file. For example:
- If your HTML is in a
pagesfolder, the path might need to be../vanilla-tilt.js(to go up one directory) - Open your browser’s DevTools (F12) → Network tab, and confirm the script loads without a 404 error. If it’s missing, fix the file path.
4. Test with a Minimal Isolated Example
If the above steps don’t work, create a tiny test file to rule out conflicts with other code on your site:
<!DOCTYPE html> <html> <head> <style> .about-indiamain{ margin: 50px; padding: 20px; width: fit-content; background: yellowgreen; transform-style: preserve-3d; perspective: 1000px; } </style> </head> <body> <div class="about-indiamain">Main Content</div> <script src="vanilla-tilt.js"></script> <script> VanillaTilt.init(document.querySelector('.about-indiamain')); </script> </body> </html>
If the tilt works here, the issue is likely conflicting CSS or JavaScript in your main website.
内容的提问来源于stack exchange,提问作者Veer Shah

