0

我编写了一个 Javascript/jQuery 函数,用于将 HTML5 数据属性动态写入一些 HTML 标签并使Skrollr 插件工作。我无法使用 jQuery .data() 因为它只存储(它不写在标签内)属性。这是我的代码:

parallaxData();
window.addEventListener('resize', parallaxData);

function parallaxData(){

    if ($(window).width() > 768 && imgContainer.length) { 

        var imgContainerJS = document.getElementById('big-image'),
            captionJS = document.getElementById('caption'),
            headerJS = document.getElementById('header');        

        imgContainerJS.dataset.top = 'transform:translateY(0px);'
        imgContainerJS.dataset.topBottom = 'transform:translateY(' + '-' + imgHeight/4 + 'px);' 
        captionJS.dataset.anchorTarget = "#big-image-wrap"
        captionJS.dataset.top = 'transform:translateY(0px);' 
        captionJS.dataset.topBottom = 'transform:translateY(' + '-' + imgHeight/8 + 'px);'
        headerJS.dataset.anchorTarget = "#big-image-wrap"
        headerJS.dataset.top = 'transform:translateY(0px);' 
        headerJS.dataset.topBottom = 'transform:translateY(' + '-' + imgHeight/4 + 'px);' 

        var animDone = false;
        skrollr.init({
            forceHeight: false,
            smoothScrolling: false,     
            render: function() {
                if ( header.hasClass('skrollable-after') ) {
                    if ( ! animDone ) {
                        animDone = true;
                        header.addClass('fixed-header').css({
                            'display' : 'none'
                        }).fadeIn(300);   
                    }
                } else {
                    animDone = false;
                    header.removeClass('fixed-header');
                }
            }           
            }).refresh();

        imgCaption.css({ position: 'fixed' });
        singleImg.css({ position: 'fixed' });

    } else if ($(window).width() > 768) {

        $('#content').css({ marginTop: headerHeight + 'px' });
        imgCaption.css({ position: 'fixed' });
        singleImg.css({ position: 'fixed' });

    } else {

        skrollr.init().destroy();
        $('#content').css({ marginTop: 0 + 'px' });

        var parallaxEls = $('header, #big-image, #caption'),
            attrs = parallaxEls[0].attributes,
            name,
            index;
        for (index = attrs.length - 1; index >= 0; --index) {
            name = attrs[index].nodeName;
            if (name.substring(0, 5) === "data-") {
                parallaxEls.removeAttr(name);
            }
        }   

        parallaxEls.css({
            '-webkit-transform' : '',
            '-moz-transform' : '',
            'transform' : '',
            'backgroundPosition' : ''
        }).removeClass('skrollable-after');

        imgCaption.css({ position: 'absolute' });
        singleImg.css({ position: 'absolute' });

    }

}

我想知道是否有机会仅使用 jQuery 来实现相同的结果,还因为我需要按类而不是 ID 选择元素。

4

1 回答 1

0

我用 jQuery 试过了。可以将属性添加到 img 或 div 或其他任何东西。

http://api.jquery.com/attr/#attr-attributeName-value

$(document).ready(function(){
    $(".yourclass").attr({
         "data-450": "exampleCSS:examplechange;",
         "data-500": "exampleCSS:examplechange;"});
});

我用我的代码中的一个 div 进行了尝试,该元素不可见,但添加了属性,我可以在 firebug 中看到它


其他 div 仍在滚动,所以我在其他地方搞砸了。


希望它对您有所帮助(即使您几个月前问过)。如果您已经找到答案,请分享您的解决方案

于 2014-02-05T11:45:16.940 回答