0

このチュートリアルでウェブサイトを構築しています。

コンテンツの高さに基づいて自動高さを追加する方法を見つけようとしています。元々はウィンドウのサイズにロックされていましたが、さらに下に拡大したいです。

元のコードの 455 行目:

        calcHeight : function() {

        var heightPreview = winsize.height - this.$item.data( 'height' ) - marginExpanded,
            itemHeight = winsize.height;

        if( heightPreview < settings.minHeight ) {
            heightPreview = settings.minHeight;
            itemHeight = settings.minHeight + this.$item.data( 'height' ) + marginExpanded;
        }

        this.height = heightPreview;
        this.itemHeight = itemHeight;

    },

50% の確率で動作する私のソリューション (かなり良いローカルで動作しますか?!):

        calcHeight : function() {
        var lol = this.$fullimage[0].getElementsByTagName('img')[0];
        var oImage = new Image();

        oImage.onload = function(){
            this.height// width of loaded image
        }

        oImage.src = lol.src;
        this.height = oImage.height;
        this.itemHeight = oImage.height+650;
    },
    setHeights : function() {

        var self = this,
            onEndFn = function() {
                if( support ) {
                    self.$item.off( transEndEventName );
                }
                self.$item.addClass( 'og-expanded' );
            };
4

2 に答える 2

1

どうぞ。これはあなたが達成しようとしていることに役立つと思います。基本的に、テキストの長さに関係なく、div の高さはコンテンツの高さ、または最初の画像の高さのいずれか大きい方になります。次に、div をクリックすると、追加の画像が読み込まれます。画像が読み込まれた後、div をもう一度クリックすると、縮小して元のサイズに戻ります。

それをチェックして、微調整が必​​要な場合はお知らせください。

注: これはブラウザー ウィンドウの方が見栄えがよくなりますが、jsfiddle では機能します。

http://jsfiddle.net/fLPM5/1/

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <style>
            .story {
                width:75%;
                padding:5px;
                margin:5px;
                cursor:pointer;
                border:1px solid black;
            }
            .text{
                padding:5px;
                margin:5px;
                width:20%;
            }
            .images{
                float:right;
                padding:5px;
                margin:5px;
                width:70%;
            }
            img{
                padding:5px;
                margin:5px;
                width:95%;
            }
        </style>
    </head>
    <body>
        <div class="story" id="story1">
            <div class="images">
                <img src="http://www.gstatic.com/webp/gallery/1.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/2.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/3.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/4.jpg" />
            </div>
            <div class="text">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit fusce vel sapien elit in malesuada semper mi, id sollicitudin urna fermentum ut fusce varius nisl ac ipsum gravida vel pretium tellus tincidunt integer eu augue augue nunc elit dolor, luctus placerat scelerisque euismod, iaculis eu lacus nunc mi elit, vehicula ut.Lorem ipsum dolor sit amet, consectetur adipiscing elit fusce vel sapien elit in malesuada semper mi, id sollicitudin urna fermentum ut fusce varius nisl ac ipsum gravida vel pretium tellus tincidunt integer eu augue augue nunc elit dolor, luctus placerat scelerisque euismod, iaculis eu 100% Original Work created by Howard Renollet for StackOverflow.com http:// stackoverflow.com /questions /19916419 /expanding-preview-calcheight-function/ ipsum dolor sit amet, consectetur adipiscing elit fusce vel sapien elit in malesuada semper mi, id sollicitudin urna fermentum ut fusce varius nisl ac ipsum gravida vel pretium tellus tincidunt integer eu augue augue nunc elit dolor, luctus placerat scelerisque euismod, iaculis eu lacus nunc mi elit, vehicula ut.Lorem ipsum dolor sit amet, consectetur adipiscing elit fusce vel sapien elit in malesuada ut fusce varius nisl ac ipsum gravida vel pretium tellus tincidunt integer eu augue augue nunc elit dolor, luctus placerat scelerisque euismod, iaculis eu lacus nunc mi elit, vehicula ut.</p>
            </div>
        </div>
        <div class="story" id="story2">
            <div class="images">
                <img src="http://www.gstatic.com/webp/gallery/1.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/2.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/3.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/4.jpg" />
            </div>
            <div class="text">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit fusce vel sapien elit in malesuada semper mi, id sollicitudin urna fermentum ut fusce varius nisl ac ipsum gravida vel pretium tellus.</p>
            </div>
        </div>
        <div class="story" id="story3">
            <div class="images">
                <img src="http://www.gstatic.com/webp/gallery/1.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/2.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/3.jpg" />
                <img src="http://www.gstatic.com/webp/gallery/4.jpg" />
            </div>
            <div class="text">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit fusce vel sapien elit in malesuada semper.</p>
            </div>
        </div>
        <script>
            (function () {
                //Get each story box
                $(".story").each(function () {

                    //for each .images box
                    $(this).children(".images").each(function () {
                        //hide all of the images
                        $(this).children("img").hide();
                    });

                    //show the first image
                    $(this).children(".images").children("img:first").show();

                    //get the text height
                    var textHeight = $(this).children(".text").height();

                    //get the image height
                    var imgHeight = $(this).children(".images:first-child").height();

                    //define originalHeight
                    var originalHeight;

                    //Which one is bigger?
                    if (textHeight >= imgHeight) {
                        //If the text height is larger
                        //set the story box height to the text height
                        $(this).height(textHeight);
                        //set originalHeight
                        originalHeight = textHeight;

                    }
                    else if (imgHeight > textHeight) {
                        //if the image height is larger
                        //set the story box height to the image height
                        $(this).height(imgHeight);
                        //set originalHeight
                        originalHeight = imgHeight;
                    };

                    //when clicking the div
                    $(this).click(function () {

                        //if this height is less than the image max height
                        if ($(this).height() <= originalHeight) {

                            //show the images
                            $(this).children(".images").children("img").show();

                            //set this height to the image max height
                            $(this).height($(this).children(".images").height());
                        }
                        else {
                            //for each .images box
                            $(this).children(".images").each(function () {

                                //hide all of the images
                                $(this).children("img").hide();
                            })

                            //show the first image
                            $(this).children(".images").children("img:first").show();

                            //set the text height
                            var textHeight = $(this).children(".text").height();

                            //set the image height
                            var imgHeight = $(this).children(".images:first-child").height();

                            //Which one is bigger?
                            if (textHeight >= imgHeight) {
                                //If the text height is larger

                                //set the story box height to the text height
                                $(this).height(textHeight);
                            }
                            else if (imgHeight > textHeight) {
                                //if the image height is larger

                                //set the story box height to the image height
                                $(this).height(imgHeight);
                            };
                        };
                    });
                });
            })();
        </script>
    </body>
</html>
于 2013-11-13T18:49:53.047 に答える
0

注意事項:

calcHeight : 関数() {

        var heightPreview = $('.og-details p').height() + this.$item.data( 'height' ) + marginExpanded,
            itemHeight = heightPreview;

        if( heightPreview < settings.minHeight ) {
            heightPreview = settings.minHeight;
            itemHeight = settings.minHeight + this.$item.data( 'height' ) + marginExpanded;

        }

        this.height = heightPreview;
        this.itemHeight = itemHeight + this.$item.data( 'height' ) + marginExpanded ;

    },
于 2014-07-10T08:50:59.773 に答える