jquery - SVG 的脚本有效,但不能作为回调

标签 jquery css svg

我正在处理带有内联 svg 的 html 页面。 svg 有一个“隐藏”类,g 的第一个子类有一个“frametoMove”类

我正在尝试使用 Jquery 将类更改为名为“blackframe”的类。该类有一个将第一个子 g 元素旋转 20 度的 css 动画。

我可以使用 .attr() 函数使它工作,但它仅在它遵循隐藏整个 svg 代码的函数(因此给它类“隐藏”)时才起作用

 $(document).ready(function() {
        /* Every time the window is scrolled ... */
        $(window).scroll(function() {
            /* Check the location of each desired element */
            $('.hideme').each(function(i) {
                var bottom_of_object = $(this).offset().top + $(this).outerHeight();
                var bottom_of_window = $(window).scrollTop() + $(window).height();
                /* If the object is completely visible in the window, fade it it */
                if (bottom_of_window > bottom_of_object) {
                    $(this).animate({
                            'opacity': '1'
                        }, 2000);
                        //works this way, but adds attr to all g:first-child elements at the same time
                            $('.hideme').children('g:first-child').attr("class", "frametoMove blackframe");

                };
            });
        });
        });    
$(document).ready(function() {
        /* Every time the window is scrolled ... */
        $(window).scroll(function() {
            /* Check the location of each desired element */
            $('.hideme').each(function(i) {
                var bottom_of_object = $(this).offset().top + $(this).outerHeight();
                var bottom_of_window = $(window).scrollTop() + $(window).height();
                /* If the object is completely visible in the window, fade it it */
                if (bottom_of_window > bottom_of_object) {
                    $(this).animate({
                            'opacity': '1'
                        }, 2000),
//doesn’t work as callback

                        function() {
                            $('.hideme').children('g:first-child').attr("class", "frametoMove blackframe");
                        };
                };
            });
        });
        });   

最佳答案

当设置 class 时,您要对所有 hideme 元素的第一个子元素进行设置。将代码更改为

 $(this)
   .children('g:first-child')
   .attr("class", "frametoMove blackframe");

由于早期的右括号,回调不起作用。改为

$(this).animate(
   {'opacity': '1'}, 
   2000,
   function() {
     $(this)
     .children('g:first-child')
     .attr("class", "frametoMove blackframe");
   });

关于jquery - SVG 的脚本有效,但不能作为回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32552656/

相关文章:

javascript - 在客户端上传多个文件并压缩,然后在服务器上上传压缩文件

css - 找不到正确的 Bootstrap 3 下拉菜单状态选择器

css - 较少的递归混合函数无法正常工作

php - 使背景图像 SVG 像内联元素一样工作

css - SVG 线标记在 IE 中的缩放比例不同

javascript - 作为加载屏幕的指令

javascript - 查找带有特定文本的 td,并在该文本之后立即对 td 进行操作?

javascript - 使用 fullpage.js 在部分之间滚动时如何固定文本动画更改,然后通过某个部分消失

css - 在 SVG 使用实例中传递自定义属性

javascript - JqG​​rid 格式化程序按钮(操作)不可见