Django中AnythingZoomer放大镜功能无法生效问题求助
解决Django中AnythingZoomer放大镜功能失效的问题
我来帮你一步步排查并修复这个问题:
1. 修复HTML结构的无效写法
你的#zoom容器写了两个class属性,这不符合HTML规范,浏览器只会识别第一个class,第二个会被忽略。同时要给缩放源添加插件默认识别的.small类:
<div id="zoom" class="row zoom"> <div class="col-md-12 small"> <p>La memoria es una de las principales facultades humanas, nos define y nos hace únicos, recogiendo y reteniendo cada una de las experiencias vividas. Debemos proteger este tesoro para que la biografía perdure más allá de la propia persona.</p> </div> </div>
.small类是AnythingZoomer默认的缩放源标识,开启clone: true时,插件会自动克隆这个区域生成放大版的.large容器。
2. 调整脚本加载顺序(关键!)
你现在的脚本执行顺序是先跑自己的jQuery代码,再加载jQuery库和插件,这会直接导致$未定义,代码根本无法执行。正确的加载顺序必须是:
- 加载jQuery库
- 加载AnythingZoomer插件
- 执行你的初始化脚本
修改base.html中的代码顺序:
<link rel="stylesheet" href="{% static 'css/anythingzoomer.css' %}"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="{% static 'js/jquery.anythingzoomer.js' %}"></script> <script> $(function() { // clone the small area, the css will define the dimensions // default class name for the large area is "large" $("#zoom").anythingZoomer({ clone: true }); // zoom in $('button').click(function(){ $(this).addClass('selected').siblings().removeClass('selected'); // .small p { font-size: 8px; width: 300px; } var origW = 300, origSz = 8, // get multiple multiple = parseInt( $(this).text() ); $('.large p').css({ width: origW * multiple, fontSize: origSz * multiple }); // update anythingZoomer window, // but first turn off clone since cloning the small area again will remove our changes $('#zoom').getAnythingZoomer().options.clone = false; $('#zoom').anythingZoomer(); }); }); </script>
3. 补充基础样式定义
你提供的CSS缺少对.small和.large区域的基础尺寸定义,插件需要这些参数来计算放大比例。在anythingzoomer.css末尾添加:
/* 缩放源基础样式,与脚本中的origW、origSz对应 */ .small { width: 300px; } .small p { font-size: 8px; margin: 0; line-height: 1.2; } /* 放大区域适配样式 */ .large p { line-height: 1.2; }
4. 验证静态资源加载
打开浏览器开发者工具(F12)的「网络」标签,刷新页面,确认anythingzoomer.css和jquery.anythingzoomer.js都返回200状态码。如果是404,说明Django的静态资源配置有问题,需要检查STATIC_URL和STATICFILES_DIRS的设置是否正确。
5. 先测试基础功能
可以先简化初始化代码,去掉按钮缩放逻辑,验证基础放大镜是否能正常工作:
$(function() { $("#zoom").anythingZoomer({ clone: true, zoomWidth: 200, // 手动指定放大镜窗口宽度 zoomHeight: 200 // 手动指定放大镜窗口高度 }); });
如果基础放大镜能正常显示,再逐步加回按钮缩放的逻辑。
内容的提问来源于stack exchange,提问作者eneko valero
相关产品推荐
相关产品推荐

