如果我们使用溢出,则部分弹出显示:隐藏

时间:2019-04-24 10:11:11

标签: javascript html css html5

我们正在容器中显示文本,如下所示:

enter image description here

Onclick文本,我们正在做以下事情:

1。允许用户添加更多文本

2。显示弹出

enter image description here

问题

当用户添加更多文本时,它显示在容器之外,但是我们想隐藏显示在容器之外的文本。...

因此我们为容器添加了overflow: hidden;,但现在仅显示 partial 弹出框,如下所示。...

enter image description here

//  Close text pop up onclick outside

$(document).mouseup(function(e) {
    var container = $('.white_content, .font-select');

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        container.hide();
        $('.font-select').css('display', 'none');
    }
});

// end

var className;

// Display pop up next to text

function closePopUp(el) {
    el.style.display = 'none';
    $('.font-select').css('display', 'none');
    openID = null
}

function openPopUp(el) {
    // console.log(el);
    if (openID != null) {
        closePopUp(openID)
    }

    $('.font-select:first').css({
        display: 'block'
    });

    el.style.display = 'block';
    openID = el;
}



let openID = null;

var target;
const imageUrl = "https://i.imgur.com/RzEm1WK.png";

let jsonData = {   
    
    "layers": [{
        "x": 0,
        "height": 300,
        "layers": [                      
            {
                "justification": "center",
                "font": "Coustard-Black",
                "x": 190,
                "y": 150,
                "src": "0a7fb3015bb19745da114bc581e96947_Font248.ttf",
                "width": 72,
                "type": "text",
                "color": "red",
                "size": 32,
                "text": "DATE",
                "height": 17,
                "name": "edit_date"
            }
        ],
        "y": 0,
        "width": 350,
        "type": "group",
        "name": "fb_post_4"
    }]
};

const containerElement = $('#container');
const fileUp = $('#fileup');

$(function() {   

    // IGNORE BELOW CODE

    //  Fetch text from json file

    function getAllSrc(layers) {
        let arr = [];
        layers.forEach(layer => {
            if (layer.src) {
                arr.push({
                    src: layer.src,
                    x: layer.x,
                    y: layer.y,
                    height: layer.height,
                    width: layer.width,
                    name: layer.name
                });
            } else if (layer.layers) {
                let newArr = getAllSrc(layer.layers);
                if (newArr.length > 0) {
                    newArr.forEach(({
                        src,
                        x,
                        y,
                        height,
                        width,
                        name
                    }) => {
                        arr.push({
                            src,
                            x: (layer.x + x),
                            y: (layer.y + y),
                            height,
                            width,
                            name: (name)
                        });
                    });
                }
            }
        });
        return arr;
    }

    function json(data)

    {
        var width = 0;
        var height = 0;

        let arr = getAllSrc(data.layers);

        let layer1 = data.layers;
        width = layer1[0].width;
        height = layer1[0].height;
        let counter = 0;
        let table = [];

        // container dimensions
        containerElement.css('width', width + "px").css('height', height + "px").addClass('temp');
        //end

        for (let {
                src,
                x,
                y,
                name
            } of arr) {
                   
            var imageUrl1 = '';                         
            var mask = $(".container").mask({
                imageUrl: imageUrl1,

                // Fetch Mask images
                maskImageUrl: 'http://piccellsapp.com:1337/parse/files/PfAppId/' + src,
                // end

                onMaskImageCreate: function(img) {
                    // Mask image positions
                    img.css({
                        "position": "absolute",
                        "left": x + "px",
                        "top": y + "px"
                    });
                    // end
                },
                id: counter
            });
            table.push(mask);
            fileup.onchange = function() {

                let mask2 = table[target];
                const newImageLoadedId = mask2.loadImage(URL.createObjectURL(fileup.files[0]));
                document.getElementById('fileup').value = "";               
            };
            counter++;
        }

        // Text
        drawText(data);
    }
    json(jsonData);
}); // end of function

// IGNORE ABOVE CODE

//Fetch text

const fonts = []; // caching duplicate fonts

function drawText(layer) {

    if (layer.type === 'image') return;

    if (!layer.type || layer.type === 'group') {
        return layer.layers.forEach(drawText)
    }

    if (layer.type === 'text') {
        const url = 'https://i.imgur.com/' + layer.src;

        if (!fonts.includes(url)) {
            fonts.push(url);
            $("style").prepend("@font-face {\n" +
                "\tfont-family: \"" + layer.font + "\";\n" +
                "\tsrc: url(" + url + ") format('truetype');\n" +
                "}");
        }

        // Below is POP UP Code
        const lightId = 'light' + layer.name
        const lightIdString = '#' + lightId
        $('.container').append(
            '<input id="font" style="display:none"><a id ="' + layer.name + '" onclick="openPopUp(' + lightId + ')"' +
            '<div class="txtContainer" id = "text" contenteditable="true" ' +
            'style="' +
            'left: ' + layer.x + 'px; ' +
            'top: ' + layer.y + 'px; ' +
            'font-size: ' + layer.size + 'px; ' +
            '">' + layer.text + '</div></a>' +
            '<div id="light' + layer.name + '" class="white_content" style="' +
            'left: ' + layer.x + 'px; ' +
            'top: ' + layer.y + 'px; ' + '"> ' +          
            '<a class = "close" href="javascript:void(0)" onclick="closePopUp(' + lightId + ')">Close this pop up box</a></div> <div>'
        );    
		
		setInterval(function() {
            document.getElementById(lightId).style.left = layer.x + document.getElementById(layer.name).offsetWidth + 'px';
         }, 100);
	}

}
// text end

// IGNORE BELOW CODE 

(function($) {
    var JQmasks = [];
    $.fn.mask = function(options) {
        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            maskImageUrl: undefined,
            imageUrl: undefined,
            scale: 1,
            id: new Date().getUTCMilliseconds().toString(),
            x: 0, // image start position
            y: 0, // image start position
            onMaskImageCreate: function(div) {},
        }, options);


        var container = $(this);

        let prevX = 0,
            prevY = 0,
            draggable = false,
            img,
            canvas,
            context,
            image,
            timeout,
            initImage = false,
            startX = settings.x,
            startY = settings.y,
            div;

        
        container.updateStyle = function() {
            return new Promise((resolve, reject) => {
                context.beginPath();
                context.globalCompositeOperation = "source-over";
                image = new Image();
                image.setAttribute('crossOrigin', 'anonymous');
                image.src = settings.maskImageUrl;
                image.onload = function() {
                    canvas.width = image.width;
                    canvas.height = image.height;
                    context.drawImage(image, 0, 0, image.width, image.height);
                    div.css({
                        "width": image.width,
                        "height": image.height
                    });
                    resolve();
                };
            });
        };        
    };
}(jQuery));
.container {
	background: silver;
	position: relative;
	overflow:hidden;
}

.container img {
	position: absolute;
	top: 0;
	bottom: 250px;
	left: 0;
	right: 0;
	margin: auto;
	z-index: 999;
}

.masked-img {
	overflow: hidden;
	position: relative;
}

.txtContainer {
	position: absolute;
	color: #FFF;
	white-space: pre;
}

.txtContainer:hover {
	position: absolute;
	background: red;
	white-space: pre;
}

.pip {
	display: inline-block;
	margin: 0;
	position: absolute;
}

.white_content {
	display: none;
	position: absolute;
	top: 25%;
	left: 25%;
	width: 21%;
	height: 40%;
	padding: 16px;
	border: 16px solid orange;
	background-color: white;
	z-index: 1002;
	overflow: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body style="background : green">
<input id="fileup" name="fileup" type="file" style="display:none" >

<div id="container"class="container">
</div>

Codepen:https://codepen.io/kidsdial/pen/wZYjbE

2 个答案:

答案 0 :(得分:2)

您必须在max-width中应用.txtContainer,然后从容器类中删除position: relative;

通过删除position属性,div将排在最前面,通过添加max-width,锚标记将不会增加。

只需更新CSS类以上

.txtContainer {
    position: absolute;
    color: #FFF;
    white-space: pre;
    max-width: 50px;
}

.container {
    background: silver;
    /* position: relative; */
    overflow: hidden;
}

答案 1 :(得分:1)

使用拖动功能更新答案:

//  Close text pop up onclick outside

$(document).mouseup(function(e) {
    var container = $('.white_content, .font-select');

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        container.hide();
        $('.font-select').css('display', 'none');
    }
});

// end

var className;

// Display pop up next to text

function closePopUp(el) {
    el.style.display = 'none';
    $('.font-select').css('display', 'none');
    openID = null
}

function openPopUp(el) {
    // console.log(el);
    if (openID != null) {
        closePopUp(openID)
    }

    $('.font-select:first').css({
        display: 'block'
    });

    el.style.display = 'block';
    openID = el;
}



let openID = null;

var target;
const imageUrl = "https://i.imgur.com/RzEm1WK.png";

let jsonData = {   
    
    "layers": [{
        "x": 0,
        "height": 300,
        "layers": [                      
            {
                "justification": "center",
                "font": "Coustard-Black",
                "x": 0,
                "y": 0,
                "src": "0a7fb3015bb19745da114bc581e96947_Font248.ttf",
                "width": 72,
                "type": "text",
                "color": "red",
                "size": 32,
                "text": "DATE",
                "height": 17,
                "name": "edit_date"
            }
        ],
        "y": 0,
        "width": 350,
        "type": "group",
        "name": "fb_post_4"
    }]
};

const containerElement = $('#container');
const fileUp = $('#fileup');

$(function() {   

    // IGNORE BELOW CODE

    //  Fetch text from json file

    function getAllSrc(layers) {
        let arr = [];
        layers.forEach(layer => {
            if (layer.src) {
                arr.push({
                    src: layer.src,
                    x: layer.x,
                    y: layer.y,
                    height: layer.height,
                    width: layer.width,
                    name: layer.name
                });
            } else if (layer.layers) {
                let newArr = getAllSrc(layer.layers);
                if (newArr.length > 0) {
                    newArr.forEach(({
                        src,
                        x,
                        y,
                        height,
                        width,
                        name
                    }) => {
                        arr.push({
                            src,
                            x: (layer.x + x),
                            y: (layer.y + y),
                            height,
                            width,
                            name: (name)
                        });
                    });
                }
            }
        });
        return arr;
    }

    function json(data)

    {
        var width = 0;
        var height = 0;

        let arr = getAllSrc(data.layers);

        let layer1 = data.layers;
        width = layer1[0].width;
        height = layer1[0].height;
        let counter = 0;
        let table = [];

        // container dimensions
        containerElement.css('width', width + "px").css('height', height + "px").addClass('temp');
        //end

        for (let {
                src,
                x,
                y,
                name
            } of arr) {
                   
            var imageUrl1 = '';                         
            var mask = $(".container").mask({
                imageUrl: imageUrl1,

                // Fetch Mask images
                maskImageUrl: 'http://piccellsapp.com:1337/parse/files/PfAppId/' + src,
                // end

                onMaskImageCreate: function(img) {
                    // Mask image positions
                    img.css({
                        "position": "absolute",
                        "left": x + "px",
                        "top": y + "px"
                    });
                    // end
                },
                id: counter
            });
            table.push(mask);
            fileup.onchange = function() {

                let mask2 = table[target];
                const newImageLoadedId = mask2.loadImage(URL.createObjectURL(fileup.files[0]));
                document.getElementById('fileup').value = "";               
            };
            counter++;
        }

        // Text
        drawText(data);
    }
    json(jsonData);
}); // end of function

// IGNORE ABOVE CODE

//Fetch text

const fonts = []; // caching duplicate fonts

function drawText(layer) {

    if (layer.type === 'image') return;

    if (!layer.type || layer.type === 'group') {
        return layer.layers.forEach(drawText)
    }

    if (layer.type === 'text') {
        const url = 'https://i.imgur.com/' + layer.src;

        if (!fonts.includes(url)) {
            fonts.push(url);
            $("style").prepend("@font-face {\n" +
                "\tfont-family: \"" + layer.font + "\";\n" +
                "\tsrc: url(" + url + ") format('truetype');\n" +
                "}");
        }

        // Below is POP UP Code
        const lightId = 'light' + layer.name
        const lightIdString = '#' + lightId
        $('#container').append(
            '<input id="font" style="display:none"><a id ="' + layer.name + '" onclick="openPopUp(' + lightId + ')"' +
            '<div class="txtContainer" id = "text" contenteditable="true" ' +
            'style="' +
            'left: ' + layer.x + 'px; ' +
            'top: ' + layer.y + 'px; ' +
            'font-size: ' + layer.size + 'px; ' +
            '">' + layer.text + '</div></a>' 
        );   
      $(".control-container").append('<div id="light' + layer.name + '" class="white_content" style="' +
            'top: ' + layer.y + 'px; ' + '"> ' +          
            '<a class = "close" href="javascript:void(0)" onclick="closePopUp(' + lightId + ')">Close this pop up box</a></div> <div>');
		
		setInterval(function() {
            document.getElementById(lightId).style.left = layer.x + document.getElementById(layer.name).offsetWidth + 'px';
         }, 100);
	}

}
// text end

// IGNORE BELOW CODE 

(function($) {
    var JQmasks = [];
    $.fn.mask = function(options) {
        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            maskImageUrl: undefined,
            imageUrl: undefined,
            scale: 1,
            id: new Date().getUTCMilliseconds().toString(),
            x: 0, // image start position
            y: 0, // image start position
            onMaskImageCreate: function(div) {},
        }, options);


        var container = $(this);

        let prevX = 0,
            prevY = 0,
            draggable = false,
            img,
            canvas,
            context,
            image,
            timeout,
            initImage = false,
            startX = settings.x,
            startY = settings.y,
            div;

        
        container.updateStyle = function() {
            return new Promise((resolve, reject) => {
                context.beginPath();
                context.globalCompositeOperation = "source-over";
                image = new Image();
                image.setAttribute('crossOrigin', 'anonymous');
                image.src = settings.maskImageUrl;
                image.onload = function() {
                    canvas.width = image.width;
                    canvas.height = image.height;
                    context.drawImage(image, 0, 0, image.width, image.height);
                    div.css({
                        "width": image.width,
                        "height": image.height
                    });
                    resolve();
                };
            });
        };        
    };
}(jQuery));

$( function() {
    $(".txtContainer").draggable();
  $(".control-container").draggable();
  } );
.container {
	background: silver;
	position: relative;
  overflow:hidden;
}

.container img {
	position: absolute;
	top: 0;
	bottom: 250px;
	left: 0;
	right: 0;
	margin: auto;
	z-index: 999;
}

.masked-img {
	overflow: hidden;
	position: relative;
}

.txtContainer {
	position: absolute;
	color: #FFF;
  overflow-wrap: initial;
  white-space: nowrap;
  text-align: start;
}

.txtContainer:hover {
	position: absolute;
	background: red;
}

.pip {
	display: inline-block;
	margin: 0;
	position: absolute;
}

.white_content {
	display: none;
	position: absolute;
	top: 25%;
	left: 360px!important;
    width: 100px;
    height: 200px;
	padding: 16px;
	border: 16px solid orange;
	background-color: white;
	z-index: 1002;
	overflow: auto;
}

.control-container{
  width:100px;
  top:-300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<body style="background : green">
<input id="fileup" name="fileup" type="file" style="display:none" >
  <h5>NOTE : drag the elements to place you want.</h5>
<div id="container"class="container"></div>
<div class="control-container"></div>

https://codepen.io/ContexDev/pen/ROeBeP