2017-12-13 81 views
6

का उपयोग कर निश्चित मूल्य द्वारा आकार बदलें मैं उपयोगकर्ताओं को अपनी सामग्री का आकार बदलने के लिए सीएसएस resize:both; संपत्ति का उपयोग कर रहा हूं। नीचे दिया गया कोड देखें।सीएसएस

.foo { 
 
    resize: both; 
 
    min-width: 250px; 
 
    width: auto; 
 
    text-align: center; 
 
    min-height: 30px; 
 
    border: 1px solid #515151; 
 
    border-radius: 6px; 
 
    position: absolute; 
 
    padding: 0; 
 
    overflow: hidden; 
 
    text-shadow: 1px 1px 1px rgba(0,0,0,0.33); 
 
    box-shadow: 5px 5px rgba(0,0,0, 0.1); 
 
} 
 
.thClass { 
 
    margin-left: -3px; 
 
    text-align: center; 
 
    box-shadow: 0 2px 2px rgba(0,0,0,0.26); 
 
    -webkit-box-shadow: 0 2px 2px rgba(0,0,0,0.26); 
 
    -moz-box-shadow: 0 2px 2px rgba(0,0,0,0.26); 
 
    margin-top: -2px; 
 
    min-height: 30px; 
 
    line-height: 30px; 
 
    font-size: 19px; 
 
    cursor: move; 
 
} 
 
.header{ 
 
    margin-left: 17px; 
 
} 
 
.tableBody { 
 
    display: block; 
 
    min-height: 25px; 
 
    text-align: center; 
 
    width: 102%; 
 
    margin-left: -2px; 
 
    cursor: default; 
 
} 
 
.foo tbody tr td { 
 
    display: block; 
 
    line-height: 25px; 
 
    text-align: center; 
 
    border-bottom: 1px solid rgba(0,0,0,0.2); 
 
} 
 
#displaySizes { 
 
    list-style: none; 
 
    padding: 0; 
 
    margin-top: 0; 
 
} 
 
.disp tbody tr th { 
 
    border: 1px solid black; 
 
} 
 
.disp tbody tr td { 
 
    display: table-cell; 
 
}
<table class="foo disp elementTable"> 
 
       <tr class="tableHeader"> 
 
        <th class="thClass" colspan="5"> 
 
         <span class="header">Device</span> 
 
        </th> 
 
       </tr> 
 
       <tr> 
 
        <td rowspan="4" id="sizeContainer"> 
 
         <ul id="displaySizes"> 
 
          <li>4:3</li> 
 
          <li>16:9</li> 
 
          <li>Clock</li> 
 
         </ul> 
 
        </td> 
 
        <td>$100</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
       <tr> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
       <tr> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
       <tr> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
      </table>

वहाँ सीएसएस का उपयोग कर, उदाहरण के लिए, [10,10] पिक्सल द्वारा कदम से इस तालिका कदम आकार बदलने के लिए कोई तरीका है? जावास्क्रिप्ट भी ठीक है। मैंने वेब की खोज की है, लेकिन कुछ भी नहीं मिला। कोड के साथ खेलने के लिए यहां fiddle काम कर रहा है।

+3

आप तालिका उपयोगकर्ता की खींचें गति के अलावा अन्य एक सेट दर पर आकार बदलने के लिए करना चाहते हैं? यदि ऐसा है तो उपयोगकर्ता को बॉलज़ के रूप में भ्रमित कर दिया जाएगा क्योंकि यह आकार बदलने जैसा प्रतीत होता है यदि यह उपयोगकर्ता की ड्रैग गति के साथ समन्वयित नहीं है। – Nope

+0

@ फ्रान सहमत, जो अजीब लग सकता है। लेकिन कारण यह है कि मैं ऐसा करना चाहता हूं कि मेरे पास ग्रिड होगा, और मैं नहीं चाहता कि ये टेबल दृश्य को बर्बाद कर दें। तो अगर मेरा ग्रिड उदाहरण के लिए 100x100 पिक्सल है, तो मैं चरण [100,100] सेट करूंगा और ग्रिड अच्छी लग रही होगी। –

+5

मुझे लगता है कि आपको यह अच्छी तरह से पहले से ही काम कर रहा है, खासकर उपयोगकर्ता के दृष्टिकोण से। यदि आप आकार को स्नैप करना चाहते हैं तो आकार के बाद आकार * आकार * को स्नैप करें। यह उपयोगकर्ता के लिए बहुत अच्छा लगेगा और यह भी आपको देगा कि आप क्या कर रहे हैं। – Archer

उत्तर

2

एक अच्छा बात है, तो यह css-element-queries लाइब्रेरी, का उपयोग कर सकता है यही कारण है कि आप सभी के लिए है अपनी मेज के लिए एक ResizeSensor() पैदा कर रही है और फिर दोनों ऊंचाई और चौड़ाई रेंज परिवर्तन स्थापित करने के लिए गणना कर:

स्निपेट नीचे देखें:

var width = $('.elementTable').width(); 
 
var height = $('.elementTable').height(); 
 
var changePX = 50; 
 

 

 
new ResizeSensor(jQuery('.elementTable'), function(){ 
 

 
    //width calcuation 
 
    if(Math.abs($('.elementTable').width()-width) > changePX) { 
 
     $('.elementTable').width() > width ? width +=changePX : width -=changePX; 
 
    } 
 
    $('.elementTable').width(width); 
 
    
 
    //height calcuation 
 
    if(Math.abs($('.elementTable').height()-height) > changePX) { 
 
     $('.elementTable').height() > height ? height +=changePX : height -=changePX; 
 
    } 
 
    $('.elementTable').height(height); 
 
    
 
})
.foo { 
 
    resize: both; 
 
    min-width: 250px; 
 
    width: auto; 
 
    text-align: center; 
 
    min-height: 30px; 
 
    border: 1px solid #515151; 
 
    border-radius: 6px; 
 
    position: absolute; 
 
    padding: 0; 
 
    overflow: hidden; 
 
    text-shadow: 1px 1px 1px rgba(0,0,0,0.33); 
 
    box-shadow: 5px 5px rgba(0,0,0, 0.1); 
 
} 
 
.thClass { 
 
    margin-left: -3px; 
 
    text-align: center; 
 
    box-shadow: 0 2px 2px rgba(0,0,0,0.26); 
 
    -webkit-box-shadow: 0 2px 2px rgba(0,0,0,0.26); 
 
    -moz-box-shadow: 0 2px 2px rgba(0,0,0,0.26); 
 
    margin-top: -2px; 
 
    min-height: 30px; 
 
    line-height: 30px; 
 
    font-size: 19px; 
 
    cursor: move; 
 
} 
 
.header{ 
 
    margin-left: 17px; 
 
} 
 
.tableBody { 
 
    display: block; 
 
    min-height: 25px; 
 
    text-align: center; 
 
    width: 102%; 
 
    margin-left: -2px; 
 
    cursor: default; 
 
} 
 
.foo tbody tr td { 
 
    display: block; 
 
    line-height: 25px; 
 
    text-align: center; 
 
    border-bottom: 1px solid rgba(0,0,0,0.2); 
 
} 
 
#displaySizes { 
 
    list-style: none; 
 
    padding: 0; 
 
    margin-top: 0; 
 
} 
 
.disp tbody tr th { 
 
    border: 1px solid black; 
 
} 
 
.disp tbody tr td { 
 
    display: table-cell; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ResizeSensor.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ElementQueries.min.js"></script> 
 
<table class="foo disp elementTable"> 
 
       <tr class="tableHeader"> 
 
        <th class="thClass" colspan="5"> 
 
         <span class="header">Device</span> 
 
        </th> 
 
       </tr> 
 
       <tr> 
 
        <td rowspan="4" id="sizeContainer"> 
 
         <ul id="displaySizes"> 
 
          <li>4:3</li> 
 
          <li>16:9</li> 
 
          <li>Clock</li> 
 
         </ul> 
 
        </td> 
 
        <td>$100</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
       <tr> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
       <tr> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
       <tr> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
        <td>February</td> 
 
       </tr> 
 
      </table>

+0

अजीब, लेकिन यह कार्य मेरे आवेदन में काम नहीं करता है। मैंने उन स्क्रिप्ट फ़ाइलों को शामिल किया है और फ़ंक्शन की प्रतिलिपि बनाई है, लेकिन ResizeSensor ट्रिगर नहीं होता है। कोई विचार? –

+0

क्या आप इस फाइल का उपयोग करने की तरह एक पहेली, या कुछ बना सकते हैं। –

+0

यहां पहेली है: https://jsfiddle.net/vaxobasilidze/cvkatuhw/1/ किसी भी आइटम को दाएं कंटेनर पर खींचें और इसका आकार बदलें। –

2

यहाँ एक समाधान मैं एक साथ रखा है कि आकार बदलने पर दिया ग्रिड पर स्नैप हो जाएगा है (source) (इस उदाहरण 20px में।)

यह "पार ब्राउज़र आकार बदलने घटना श्रोता" का उपयोग करता है, कि एक एक और डेवलपर को एक साथ रखा गया है:

असल में यह सिर्फ आकार घटना को सुनता है, तो के लिए शैलियों सेट जावास्क्रिप्ट का उपयोग कर चौड़ाई और ऊंचाई।

यहाँ फिडल:

https://jsfiddle.net/treetop1500/co8zbatz/

// utility function for rounding (20px in this case) 
function round20(x) 
{ 
    return Math.ceil(x/20)*20; 
} 

// Attach listener 
var myElement = document.getElementById('resize'), 
    myResizeFn = function(){ 
     h = round20(parseInt(this.style.height,10)); 
     w = round20(parseInt(this.style.width,10)); 
     this.style.height = h + "px"; 
     this.style.width = w + "px"; 
    }; 

addResizeListener(myElement, myResizeFn); 
+0

अच्छा और अनुकूलित फ़ंक्शन। धन्यवाद! –

+0

क्या आप इस पहेली को देख सकते हैं? https://jsfiddle.net/vaxobasilidze/ryxks2ob/ किसी भी आइटम को दाईं ओर बदलें और आकार बदलें। आपका कोड काम नहीं करता है। क्या आप समस्या को ठीक करने में मेरी मदद कर सकते हैं? मैंने एक फ़ंक्शन में आकार बदलने वाले श्रोता को पैक किया है और आइटम को गिराए जाने पर मैं इसे कॉल करता हूं। –