我不是很熟悉 javascript所以我正在寻求一些帮助.
我正在使用在Codrops上发现的脚本(3D Grid Effect – http://tympanus.net/Development/3DGridEffect/).

一切都按预期工作正常但我正在尝试根据我的需要“修改”它.

基本上,我想触发“效果”而不是点击整个容器,而是放在其中的按钮上.

我正在使用的结构是:

<section class="grid3d vertical" id="grid3d">
        <div class="grid-wrap">
            <div class="grid">
                <div class="Box"><div class="btn-click-me">Click to Show</div></div>
                <div class="Box"><div class="btn-click-me">Click to Show</div></div>
                <div class="Box"><div class="btn-click-me">Click to Show</div></div>

            </div>
        </div>
        <div class="content">
            <div>
                <div class="dummy-img"></div>
                <p class="dummy-text">Some text</p>
                <p class="dummy-text">Some more text</p>
            </div>
            <div>
                <!-- ... -->
            </div>
            <!-- ... -->
            <span class="loading"></span>
            <span class="icon close-content"></span>
        </div>
    </section>

<script>
            new grid3D( document.getElementById( 'grid3d' ) );
        </script>

脚本(js)是

/**
 * grid3d.js v1.0.0
 * http://www.codrops.com
 *
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.PHP
 * 
 * copyright 2014,Codrops
 * http://www.codrops.com
 */
;( function( window ) {

    'use strict';

    function grid3D( el,options ) {
        this.el = el;
        this.options = extend( {},this.options );
        extend( this.options,options );
        this._init();
    }

    // any options you might want to configure
    grid3D.prototype.options = {};

    grid3D.prototype._init = function() {
        // grid wrapper
        this.gridWrap = this.el.querySelector( 'div.grid-wrap' );
        // grid element
        this.grid = this.gridWrap.querySelector( 'div.grid' );
        // main grid items
        this.gridItems = [].slice.call( this.grid.children );
        // default sizes for grid items
        this.itemSize = { width : this.gridItems[0].offsetWidth,height : this.gridItems[0].offsetHeight };
        // content
        this.contentEl = this.el.querySelector( 'div.content' );
        // content items
        this.contentItems = [].slice.call( this.contentEl.children );
        // close content cross
        this.close = this.contentEl.querySelector( 'span.close-content' );
        // loading indicator
        this.loader = this.contentEl.querySelector( 'span.loading' );
        // support: support for pointer events,transitions and 3d transforms
        this.support = support.pointerevents && support.csstransitions && support.csstransforms3d;
        // init events
        this._initEvents();
    };

    grid3D.prototype._initEvents = function() {
        var self = this;

        // open the content element when clicking on the main grid items
        this.gridItems.forEach( function( item,idx ) {
            item.addEventListener( 'click',function() {
                self._showContent( idx );
            } );
        } );

        // close the content element
        this.close.addEventListener( 'click',function() {
            self._hideContent();
        } );

        if( this.support ) {
            // window resize
            window.addEventListener( 'resize',function() { self._resizeHandler(); } );

            // trick to prevent scrolling when animation is running (opening only)
            // this prevents that the back of the placeholder does not stay positioned in a wrong way
            window.addEventListener( 'scroll',function() {
                if ( self.isAnimating ) {
                    window.scrollTo( self.scrollPosition ? self.scrollPosition.x : 0,self.scrollPosition ? self.scrollPosition.y : 0 );
                }
                else {
                    self.scrollPosition = { x : window.pageXOffset || docElem.scrollLeft,y : window.pageYOffset || docElem.scrollTop };
                    // change the grid perspective origin as we scroll the page
                    self._scrollHandler();
                }
            });
        }
    };

    // creates placeholder and animates it to fullscreen
    // in the end of the animation the content is shown
    // a loading indicator will appear for 1 second to simulate a loading period
    grid3D.prototype._showContent = function( pos ) {
        if( this.isAnimating ) {
            return false;
        }
        this.isAnimating = true;

        var self = this,loadContent = function() {
                // simulating loading...
                setTimeout( function() {
                    // hide loader
                    classie.removeClass( self.loader,'show' );
                    // in the end of the transition set class "show" to respective content item
                    classie.addClass( self.contentItems[ pos ],'show' );
                },1000 );
                // show content area
                classie.addClass( self.contentEl,'show' );
                // show loader
                classie.addClass( self.loader,'show' );
                classie.addClass( document.body,'noscroll' );
                self.isAnimating = false;
            };

        // if no support just load the content (simple fallback - no animation at all)
        if( !this.support ) {
            loadContent();
            return false;
        }

        var currentItem = this.gridItems[ pos ],itemContent = currentItem.innerHTML;

        // create the placeholder
        this.placeholder = this._createPlaceholder(itemContent );

        // set the top and left of the placeholder to the top and left of the clicked grid item (relative to the grid)
        this.placeholder.style.left = currentItem.offsetLeft + 'px';
        this.placeholder.style.top = currentItem.offsetTop + 'px';

        // append placeholder to the grid
        this.grid.appendChild( this.placeholder );

        // and animate it
        var animFn = function() {
            // give class "active" to current grid item (hides it)
            classie.addClass( currentItem,'active' );
            // add class "view-full" to the grid-wrap
            classie.addClass( self.gridWrap,'view-full' );
            // set width/height/left/top of placeholder
            self._resizePlaceholder();
            var onEndTransitionFn = function( ev ) {
                if( ev.propertyName.indexOf( 'transform' ) === -1 ) return false;
                this.removeEventListener( transEndEventName,onEndTransitionFn );
                loadContent();
            };
            self.placeholder.addEventListener( transEndEventName,onEndTransitionFn );
        };

        setTimeout( animFn,25 );
    };

    grid3D.prototype._hideContent = function() {
        var self = this,contentItem = this.el.querySelector( 'div.content > .show' ),currentItem = this.gridItems[ this.contentItems.indexOf( contentItem ) ];

        classie.removeClass( contentItem,'show' );
        classie.removeClass( this.contentEl,'show' );
        // without the timeout there seems to be some problem in firefox
        setTimeout( function() { classie.removeClass( document.body,'noscroll' ); },25 );
        // that's it for no support..
        if( !this.support ) return false;

        classie.removeClass( this.gridWrap,'view-full' );

        // reset placeholder style values
        this.placeholder.style.left = currentItem.offsetLeft + 'px';
        this.placeholder.style.top = currentItem.offsetTop + 'px';
        this.placeholder.style.width = this.itemSize.width + 'px';
        this.placeholder.style.height = this.itemSize.height + 'px';

        var onEndplaceholderTransFn = function( ev ) {
            this.removeEventListener( transEndEventName,onEndplaceholderTransFn );
            // remove placeholder from grid
            self.placeholder.parentNode.removeChild( self.placeholder );
            // show grid item again
            classie.removeClass( currentItem,'active' );
        };
        this.placeholder.addEventListener( transEndEventName,onEndplaceholderTransFn );
    }

    // function to create the placeholder
    /*
    <div class="placeholder">
        <div class="front">[content]</div>
        <div class="back"></div>
    </div>
    */
    grid3D.prototype._createPlaceholder = function( content ) {
        var front = document.createElement( 'div' );
        front.className = 'front';
        front.innerHTML = content;
        var back = document.createElement( 'div' );
        back.className = 'back';
        back.innerHTML = '&nbsp;';
        var placeholder = document.createElement( 'div' );
        placeholder.className = 'placeholder';
        placeholder.appendChild( front );
        placeholder.appendChild( back );
        return placeholder;
    };

    grid3D.prototype._scrollHandler = function() {
        var self = this;
        if( !this.didScroll ) {
            this.didScroll = true;
            setTimeout( function() { self._scrollPage(); },60 );
        }
    };

    // changes the grid perspective origin as we scroll the page
    grid3D.prototype._scrollPage = function() {
        var perspY = scrollY() + getViewportH() / 2;
        this.gridWrap.style.WebkitPerspectiveOrigin = '50% ' + perspY + 'px';
        this.gridWrap.style.MozPerspectiveOrigin = '50% ' + perspY + 'px';
        this.gridWrap.style.perspectiveOrigin = '50% ' + perspY + 'px';
        this.didScroll = false;
    };

    grid3D.prototype._resizeHandler = function() {
        var self = this;
        function delayed() {
            self._resizePlaceholder();
            self._scrollPage();
            self._resizeTimeout = null;
        }
        if ( this._resizeTimeout ) {
            clearTimeout( this._resizeTimeout );
        }
        this._resizeTimeout = setTimeout( delayed,50 );
    }

    grid3D.prototype._resizePlaceholder = function() {
        // need to recalculate all these values as the size of the window changes
        this.itemSize = { width : this.gridItems[0].offsetWidth,height : this.gridItems[0].offsetHeight };
        if( this.placeholder ) {
            // set the placeholders top to "0 - grid offsetTop" and left to "0 - grid offsetLeft"
            this.placeholder.style.left = Number( -1 * ( this.grid.offsetLeft - scrollX() ) ) + 'px';
            this.placeholder.style.top = Number( -1 * ( this.grid.offsetTop - scrollY() ) ) + 'px';
            // set the placeholders width to windows width and height to windows height
            this.placeholder.style.width = getViewportW() + 'px';
            this.placeholder.style.height = getViewportH() + 'px';
        }
    }

    // add to global namespace
    window.grid3D = grid3D;

})( window );

现在,我知道我需要看的代码的“关键”部分是:

// open the content element when clicking on the main grid items
this.gridItems.forEach( function(item,idx ) {
item.addEventListener( ‘click’,function() {
item._showContent( idx ); } ); } );

那么,我的问题又是:当每个“盒子”点击div(按钮)类“click-me”时如何触发事件?

感谢所有提前(我正在努力工作几个小时……)

解决方法

看看这里的例子,

http://jsfiddle.net/y0mbn94n/

我添加了一些初始化来获得你的特定课程

// get grid buttons and then make an iterable array out of them        
    this.gridButtons = this.el.querySelectorAll('button.btn-click-me');
    this.gridButtonItems = [].slice.call(this.gridButtons);

并改变了迭代和添加监听器的功能.

// open the content element when clicking on the buttonsItems
    this.gridButtonItems.forEach(function (item,idx) {
        item.addEventListener('click',function () {
            self._showContent(idx);
        });
    });

javascript forEach – 在所有按钮上添加addEventListener的更多相关文章

  1. 详解通过focusout事件解决IOS键盘收起时界面不归位的问题

    这篇文章主要介绍了详解通过focusout事件解决IOS键盘收起时界面不归位的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  2. wordpress添加Html5的表单验证required方法小结

    这篇文章主要介绍了wordpress添加Html5的表单验证required方法小结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. 使用placeholder属性设置input文本框的提示信息

    这篇文章主要介绍了使用placeholder属性设置input文本框的提示信息,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  4. 如何在警报视图IOS中创建两个文本字段

    =========工作解决方案===========感谢下面的答案,当你只需要两个纯文本字段时解决方法分配“消息”警报视图后.将其添加到您的代码中:这将使您的警报视图内部有两个文本字段.

  5. ios – 如何在swift中使用Textfield进行UITableview?

    我想在每个单元格中创建一个带有textfields的表视图,我有一个swift文件中的自定义类:然后在我的ViewController我有这很好:但是当用户在文本框中输入文本并按下按钮时,我想将每个文本框的字符串存储在数组中.我试过了但它没有像空的那样打印出来我该如何让它工作?

  6. 当iOS中的TextField开始输入时,PlaceHolder会动画

    如何在UITextField中设置这种类型的动画?

  7. UITextView添加Placeholder(swift)

    ){super.initcommonInit()}requiredpublicinit{super.initcommonInit()}funccommonInit(){NSNotificationCenter.defaultCenter().addobserverplaceholderLabel.font=fontplaceholderLabel.textColor=placeholderColorplaceholderLabel.textAlignment=textAlignmentplaceholde

  8. Swift自定义控件--输入框

    由于自定义控件不是系统控件,所以能直接在Xcode中拖拽,首先需要拖拽自定义的父类,比如这里继承的是UIView,所以从Xcode中拖拽一个UIView到storyboard中,然后将UIView的class设置为自定义控件名称查看类中的所有方法的快捷键?

  9. 创建自注册的Swift UI 控件

    UIKit的UITextField控件的placeholder属性就是用来干这个的。方式2:NSNotificationCenterNSNotificationCenter通过UITextViewTextDidChangeNotification通知来告诉你用户在TextView中输入或删除了某些字符。一般,我们在对象的deinit方法中向NSNotificationCenter注销该对象。但是在Swift中,我们无法在扩展中使用deinit方法。TheNotificationProxyatRunTime

  10. Swift UITextField/UITextView(placeholder的制作)

    UITextField一个UITextField对象在你的界面上显示一个可编辑的文本区域。使用UITextField需要关联一个他的代理UITextFieldDelegate,在实际中,当我们不要他的代理事件的时候,我们可以不关联代理和实现代理方法。UITextView一种文本视图接受并显示多行文本。UITextView在使用子和UITextField差不多,很多时候我们需要自己做类似UITextField的placeholder,其实我们只需要知道UITextView的几个代理方法的执行顺序就大概知道怎

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部